I'm writing a plugin and I'm getting an error 400 Bad Request from admin-ajax.php. I did some poking around in the Wordpress core about this and it seems where it's getting hung up is around line 85 of admin-ajax.php which checks if the ajax action is registered or not:

if ( ! has_action( 'wp_ajax_' . $_REQUEST['action'] ) ) {
    wp_die( '0', 400 );

Here's my code:


namespace PluginName\options;
add_action('wp_ajax_collection_sync', __NAMESPACE__ . '\\handle_ajax_post_request');
function handle_ajax_post_request(){
    echo 'success';


var data = {
    action : 'collection_sync',

$.post(ajaxurl, data, function(response){
    if( response === 'success' ){
        // handle success (update progress bar, output success message w/e)
        console.log('hoo rah');
        // handle fail (update progress bar, output error message w/e)
        console.log('uh oh');

The error comes as a surprise to me because when I check the returned value from has_action('wp_ajax_collection_sync') under the line I run add_action('wp_ajax_collection_sync', __NAMESPACE__ . '\\handle_ajax_post_request'); it's return value is true. Also when I run error_log($_REQUEST['action']) directly above line 85 in admin-ajax.php it's value is collection_sync. I'm not sure if this has anything to do with the fact that I'm using a namespaced callback or not. Anyway I'm stumped and would love some help. Thanks!!

  • If this code is not only for wp-admin, add add_action('wp_ajax_nopriv_collection_sync', __NAMESPACE__ . '\\handle_ajax_post_request'); codex.wordpress.org/Plugin_API/Action_Reference/…
    – Elex
    Dec 21, 2017 at 22:07
  • Sorry I should have mentioned but this is code being run on the admin side by a plugin. So itll need to be run from the wp_ajax hook afaik. Dec 21, 2017 at 23:34
  • Have you try to dump $_REQUEST['action'] before the if statement in admin-ajax.php ? And what is return ?
    – Elex
    Dec 22, 2017 at 9:07
  • Yes thanks (i mentioned this in my question). It returns collection_sync Dec 22, 2017 at 13:07
  • Have you tried the same exact code without using the __NAMESPACE__ constant? If you are using a later version of PHP you can pass the callback directly into the second parameter of add_action. Might help you figure out if the problem lies elsewhere. Dec 27, 2017 at 15:53


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.