[wp-hackers] Ajax requests, admin-ajax.php and the WP_ADMIN constant
wp at andrewnacin.com
Wed Feb 8 20:24:44 UTC 2012
On Mon, Feb 6, 2012 at 11:02 PM, 24/7 <24-7 at gmx.net> wrote:
> I'm currently developing an app that needs some ajax requests. Suddenly I
> found myself in a weird position: I couldn't make any requests from my ajax
> callback function. Everything loaded fine, the request was sent via
> jQuery.post() and the response was `0`. As I made the request from inside a
> class, I tested if I could hook the callback from outside and it worked.
> After a lot of debugging  I found the *"error"*: is_admin() returns true
> as admin-ajax.php defines WP_ADMIN as true on top of the file. This is a
> bit weird behavior, as we got the wp_ajax_* (admin/logged-in) and the
> wp_ajax_nopriv_* (public/guest) hook. If I want to, like in my case, built
> the front-end stuff completely separated from the back end extensions, then
> I have the problem that I simply can't divide stuff. This brings me in a
> strange situation for organizing my files: I can't stick stuff together
> that belongs together.
admin-ajax.php does indeed report that it is in the admin, despite the
of wp_ajax_nopriv_*. This is intended behavior.
I imagine what you are doing is using is_admin() to decide whether or not
should include a file or attach a hook? If that is the case, you could
defined('DOING_AJAX'), or simply always attach the hook (add_action() is
cheap). If for some reason this doesn't trigger a solution for you, then I
just don't know what you're trying to do, but — at least know that the
you're seeing is intentional. It should be easy to work around.
More information about the wp-hackers