There’s only one reason you get a 400 error with admin-ajax.php: The wp_ajax_{$action}
or wp_ajax_nopriv_{$action}
action has not been hooked with an {$action}
that matches the action
data parameter sent with your request. So either your callback is not hooked, or you’re not sending the action correctly. Your code is making both errors.
- You are encoding the data sent to the server as JSON. WordPress looks for
$_REQUEST['action']
to trigger the appropriate action. When data is sent to the server as JSON,$_REQUEST
cannot be populated, so$_REQUEST['action']
does not exist, meaning that your callback is never run. You need to removeJSON.stringify()
from your code. - You are running
add_action()
forwp_ajax_myblog_ajax_request
inside a page template. This won’t work. You are sending your request towp-admin/admin-ajax.php
, but that code does not load templates, so your callback is not hooked for that request. You need to useadd_action()
inside a file that is loaded insidewp-admin/admin-ajax.php
, such as your theme’s functions.php file, or a plugin.