How to add ajax url to js using wp_add_inline_script()?

If anyone still needs it: wp_add_inline_script( ‘map-scripts’, ‘const ajax_info = ‘ . json_encode(array( ‘ajaxurl’ => admin_url(‘admin-ajax.php’), )), ‘before’ ); but, it’s good practice to use nonce as well for better security: wp_add_inline_script( ‘map-scripts’, ‘const ajax_info = ‘ . json_encode(array( ‘ajaxurl’ => admin_url(‘admin-ajax.php’), ‘nonce’ => wp_create_nonce(‘your_nonce_handler’), //the more specific the better ‘your_other_item’ => “some additional data”, … Read more

Ajax call on new site with jupiterx theme getting 400 response [closed]

You will need to use and pass a nonce when accessing admin-ajax Enqueue script with nonce: $params = array( ‘ajaxurl’ => admin_url(‘admin-ajax.php’, $protocol), ‘ajax_nonce’ => wp_create_nonce(‘any_value_here’), ); wp_localize_script( ‘my_blog_script’, ‘ajax_object’, $params ); ajaxurl – This is the absolute address, taking into account http:// or https://, to your ajax processing script. This script is in the … Read more

Contact form 7 Hide response messages after 5 seconds

you can use below code to hide message // Contact Form 7 submit event fire document.addEventListener(‘wpcf7submit’, function(event) { setTimeout(function() { jQuery(‘form.wpcf7-form’).removeClass(‘sent’); jQuery(‘form.wpcf7-form’).removeClass(‘failed’); jQuery(‘form.wpcf7-form’).addClass(‘init’); }, 1000); }, false);

AJAX action through direct link

Use WordPress’ generic POST/GET handler wp-admin/admin-post.php Don’t let the name confuse you – it accepts both GET/POST, and just like admin-ajax.php it accepts both authorised (admin) and non-authorised (public) requests. function wpse_406199_custom_action() { // Do something! // Maybe redirect back to where they came from? wp_redirect( wp_get_referer() ); exit; } // Non-logged-in requests add_action( ‘admin_post_nopriv_wpse_406199_custom_action’, … Read more

Custom AJAX Endpoint not returning any result

You need to use the defined namespace as the prefix for the add_action callbacks so that PHP knows which function it is supposed to call. More on this here, add_action in namespace not working The add_actions should look like this then, add_action(‘wp_ajax_ajax_endpoint’, __NAMESPACE__ . ‘\\ajax_endpoint’); add_action(‘wp_ajax_nopriv_ajax_endpoint’, __NAMESPACE__ . ‘\\ajax_endpoint’); When doing development work it is … Read more

WordPress ajax call returns a zero though die()

When an ajax call returns 0 this usually means that you do not have your actions set up correctly. You will want to make sure that you are hooking into wp_ajax as well as wp_ajax_nopriv add_action( ‘wp_ajax_ACTION’, ‘your-callback’ ); add_action( ‘wp_ajax_nopriv_ACTION’, ‘your-callback’ ); I would guess that the problem is that you are only hooking … Read more

Change header.php after ajax call

Your best bet would be to change the body class (either add class or remove class) with jQuery after the ajax call as a callback. $.ajax({ url: “yourloop.php”, context: document.body }).done(function() { $(this).addClass(“finished-loop”); }); http://api.jquery.com/jQuery.ajax/

Ajax call does not work for this simple code

You have a space in your function name on the action call, add_action( ‘wp_ajax_nopriv_ MyAjaxFunction’, ‘MyAjaxFunction’ ); add_action( ‘wp_ajax_ MyAjaxFunction’, ‘MyAjaxFunction’ ); …should be, add_action( ‘wp_ajax_nopriv_MyAjaxFunction’, ‘MyAjaxFunction’ ); add_action( ‘wp_ajax_MyAjaxFunction’, ‘MyAjaxFunction’ );