Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Right now for my plugin, I am using in_admin() to determine if the user is in the frontend of the site or in the admin area. However, the problem occurs when plugins use admin_ajax.php to process ajax requests.

I need a way to register hooks and plugins only when processing admin_ajax.php or in the frontend of the site. What is the best way to go about doing that?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

Check the constant DOING_AJAX. Its definition is the first working code in wp-admin/admin-ajax.php.

Example:

if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
{
    // do something
}
share|improve this answer
    
if ( defined( 'DOING_AJAX' ) ) is enough by itself. The constant is only set in admin-ajax.php so you don't need to check for a value. –  John Reid Nov 26 at 13:55
    
@JohnReid It is a global constant, anyone can set it to any value, including FALSE. –  toscho Nov 26 at 16:36
    
Good point. There's nowhere in the WP core that sets this value but I suppose that it doesn't mean that some rogue plugin might not set it to false instead. A +1 for you, sir! –  John Reid Dec 1 at 10:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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