is_admin() is a quirky function, and its accidental mis-use can cause issues with AJAX-based plugins.

The problem

Many developers assume that is_admin() will return TRUE only when a user is logged into the admin area, but that isn’t the case. According to the documentation page:

In other words, is_admin does not check whether the user is logged in.

AJAX requests

When a plugin makes a WordPress AJAX request, it uses /wp-admin/admin-ajax.php. This is technically an admin page, so is_admin() returns TRUE, even for front-end requests.

The solution

Use the wp_doing_ajax() function to check whether AJAX is being used.

Before:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

if ( is_admin() ) {

After:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

if ( is_admin() && ! wp_doing_ajax() ) {

Last updated: September 10, 2024