AJAX jQuery post frontend returning failed to load resource status 400

There’s two problems that I see. Firstly, your add_action(‘wp_ajax_nopriv_get_question’,… code is inside your template, but your template is not going to be loaded when the request is sent to wp-admin/admin-ajax.php, which does not load templates. The callback code and the code that hooks it need to go in your theme’s functions.php file. Secondly, you have … Read more

Admin-ajax.php 400 error in custom theme

It seems that you’re adding the AJAX action only if $pagenow (the current admin page/file) is edit.php or post.php, and that’s not going to work on admin-ajax.php, so you should instead add the action via the admin_init hook if you wish to add the action only on the admin side. E.g. add_action( ‘admin_init’, function () … Read more

Ajax Call 400 Bad Request error with POST request but not with GET request

Well this is very probably going to help someone else sometime so here it goes: Not setting the request to application/x-www-form-urlencoded makes the POST body behave like a string. So PHP does not recognize $_POST variables. To make that happen we need: xhttp.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’); So the whole thing goes like: xhttp.open(“POST”, ajaxurl, true); xhttp.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’); … Read more

403 error When i send ajax request on WordPress website

Short answer — how can you solve the problem: When you call check_ajax_referer(), use secure_nonce_name as the first parameter, i.e. check_ajax_referer( ‘secure_nonce_name’, ‘security’ ). A Longer Answer The first parameter (which is the nonce action) for check_ajax_referer() needs to match the first parameter for wp_create_nonce(), but in your code, they are secure-nonce-name and secure_nonce_name respectively, … Read more

WP AJAX post filter > do something with empty value

Unfortunately, the answers given did not work. I was only looking at the jQuery portion, but the issue was in the functions filter, which I did not include in my original post. However, you did push me in the direction I needed, so thank you for that! The problem was, I was always getting a … Read more

Change URL without reload Ajax

The site you linked to isn’t updating the nav menu with AJAX, rather, when you click a link they load the target via JS then swap the entire page for the new page. A little bit of trickery with pushState ensures the URL in the address bar changes and back/forward is preserved. Take a look … Read more

Get returned URL from wp_remote_post if response code is 302

You almost had it. In the $payload you need to set the default redirection param to 0/false, $payload[‘redirection’] = false; The location header is empty because it followed through. Then you will be able to get the redirection URL from the Location header. You can see the list of arguments used here

WordPress action – Pass arguments into action in an AJAX call?

You can’t pass arguments to wp_ajax_ callbacks. You need to pass the data via the data property of the AJAX request. Then you get those values with $_POST and pass them to a function: function newsletterSignupAjax() { $id = $_POST[‘id’]; $source = $_POST[‘source’]; /* Make sure to validate and sanitize those values. */ newsletterSignup( $id, … Read more

Sql formatting for post data within function

It isn’t showing wp_posts.post_content LIKE ‘%test%’ because the wildcards (%) in the prepared statement are converted to placeholder escape strings (like the {a19f0a6c3d866f7270be0f3954cbb2600270400c15f488e112449a8a131701f0} in your post) when wpdb::prepare() calls wpdb::add_placeholder_escape() which “Adds a placeholder escape string, to escape anything that resembles a printf() placeholder“, i.e. a placeholder like %s. But those placeholder escape strings will … Read more