Read wordpress admin ajax 400 bad request for more information.

I’ve been working on ajax lately. The tutorials you find on the net are all very similar and quite easy to implement. But I always get a bad request 400 on my ajax-admin.php

After a long and intensive search, I have now found out that’s because of the time of integration.

wordpress admin ajax 400 bad request

Add a search to WordPress

If I use the ‘init’-action-hook to initialize script and wp_localize_script, everything works fine. So the code itself must be correct.

But if I use e.g. ‘wp_enqeue_scripts’-action-hook I always get the bad request.

The problem with this is:

I would like to have the functions in an extra PHP and only load them if they are needed on a particular page. For this I need, for example, is_page(). But is_page() works at the earliest when I hook the function with the inclusion into the ‘parse_query’-action-hook:

So then in my-page-test-functions.php ‘init’ does not work, I suppose because init comes before parse_query.

Is there a best practices to organize this, so it works? Or how can I fix the admin-ajax.php bad request when using the ‘wp_enqeue_scripts’-action-hook?

Hard to describe, but I hope it is quite understandable.

I think the only thing missing here is that you need to move add_action(‘wp_ajax_nopriv_ajaxlogin’,’ajax_login’); outside ajax_login_init .

That code registers your Ajax handler, but when you only run it on wp_enqueue_scripts , it’s already too late and wp_ajax_nopriv_ hooks are already run.

Now it’s more clear that you only want to load the JavaScript on that particular page. This means you need to put your is_page() inside ajax_login_init() . I’ve updated the code accordingly.

Now, why didn’t your solution work?

The is_page() check meant that your functions file was only loaded on that specific page. ajax_login_init() gets called and your scripts enqueued. So far so good.

Now your script makes the ajax call. As mentioned in the comments, ajax calls are not aware of the current page you’re on. There’s a reason the file sits at wp-admin/admin-ajax.php . There’s no WP_Query and thus is_page() does not work during an ajax request.

Since that does not work, sw18_page_specific_functions() won’t do anything in an ajax context. This means your functions file is not loaded and your ajax handler does not exist.

That’s why you need to always include that functions file and move that is_page() check inside ajax_login_init() 

Searches related to wordpress admin ajax 400 bad request

You may also like