0

Recently I was debugging/hacking a plugin authored by someone else. I put a breakpoint at a spot I was interested in - a relatively complex admin page.

I noticed that even during admin-ajax calls this particular plugin still goes through all the logic to generate the page in question. Even though it is never displayed.

Is this normal practice? Shouldn't this plugin be detecting that it's an AJAX call and thus not bothering doing all that work?

Does WP always generate the whole page when doing admin-ajax calls? That doesn't seem right.

1
  • It is kinda crazy but if you think about it, WordPress has to process all the existing code just to run the AJAX call... otherwise it wouldn't know which AJAX action to trigger via the function that is hooked to it. (That is not to say this plugin you are talking about may be making it even more cumbersome for the page you mean, it really depends on how it is coded.)
    – majick
    Mar 11, 2016 at 4:20

1 Answer 1

1

This doesn't have much to do with AJAX itself, it is related to core principal of plugin/theme development - always use the latest possible hook to run your code. It sounds like the plugin in question does something on admin_init which is a very early hook that can be triggered from AJAX and XML-RPC in addition to the usual admin. A more proper hook would have probably been admin_menu or admin_notices assuming you have some general admin code that is not restricted to one page.

The idea of trying to detect what code path led to an execution of a code is violation of basic software development principals - functional code should have explicit input, OOP code should in addition refer only to the object state and nothing beyond. Your code should be written to be platform agnostic as possible, as if you might want to use it next year in drupal. In real life drupal is probably unrealistic, but the more self contained your code is the easier it is to test it with automatic unit test tools without going through the bother of setting the whole WP enviroment.

But is it possible at all? two ways come to my mind, IIRC one of the $_SERVER attributes contain the path to the php script being run, which in case of ajax will have a file name of ....\admin-ajax.php, or you can use the backtrace API of PHP to inspect the calling stack to learn where you came from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.