Trying to figure out an issue a fellow programmer is having. I was wondering if the functions.php
file get called at all when you do admin side AJAX? I know that when you do an AJAX call a part of WP gets loaded up to process the call and send back a response. Is the functions.php
file included in that?
The reason I’m asking is because he’s using the class from the Meta-Box` plugin and loading it as part of a theme instead. There is some AJAX in that class that only returns empty responses and I think its because the code that handles the response doesn’t get loaded. Is there any documentation to what gets loaded when WP handles AJAX?
admin-ajax.php
loadswp-load.php
:wp-load.php
loadswp-config.php
, and therewp-settings.php
is loaded.And here we find this:
So, yes, the themeâs
functions.php
is loaded.There is one exception in
wp-settings.php
:When
SHORTINIT
is defined asTRUE
earlier, the theme will not be loaded.So check if
SHORTINIT
isTRUE
for some reason.Another common error is the wrong usage of
is_admin()
. This is alwaysTRUE
inadmin-ajax.php
, so the following will fail:Debugging AJAX
One method as primitive as efficient is using HTTP header to debug AJAX.
Here is a simple helper function:
And this plugin shows how to use it:
It will add a button to the front end that triggers an AJAX request when clicked. The open your browserâs network console and look at the response headers for the request:
I assume your problem AJAX was working if your are logged in and it was not working in logged out status, right?
There is a function in WordPress to access AJAX-based files for non logged users:
wp_ajax_nopriv
, for example