1

I want to have my plugin check if the user is anywhere in the Admin Panel (wp-admin) before executing a script. This would include:

/wp-admin/
/wp-admin/edit.php?post_type=page
/wp-admin/themes.php
etc.

Similar to using the using the global $pagenow:

global $pagenow;
if ( $pagenow == 'upload.php' ) {
    # do something if in the Media page
}

From doing some research, it looks like the get_current_screen() function is something I need, but this is what I've come up which doesn't work:

global $my_admin_page;
$screen = get_current_screen();
if ( $screen->id = $my_admin_page ) {
    return $content;
}
  • This question might help: wordpress.stackexchange.com/questions/106895/… – czerspalace Aug 30 '16 at 23:05
  • 1
    You're looking for is_admin(). But beware it also includes admin-ajax.php unless you want that file included. For your page you might just check $_GET['page'] (options-general.php?page=?, admin.php?page=? ..) – Samuel Elh Aug 30 '16 at 23:34
  • @czerspalace and @SamuelElh both of you referring to the is_admin() helped. Thanks. Do either one of you want to post the answer for the credit and to quickly close this question? Otherwise I'll do it – Ethan Jinks O'Sullivan Aug 31 '16 at 0:50
4

You can use the is_admin function. By definition, it checks "Whether the current request is for an administrative interface page."

You might also want to verify you are not doing an AJAX call by doing

if ( ! is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
    # code here...
}
  • Thanks for the answer. What's the reasoning to also check !DOING_AJAX? Is that to cover additional pages in the /wp-admin? – Ethan Jinks O'Sullivan Aug 31 '16 at 15:54
  • 1
    As mentioned by @Samuel Elh, is_admin also is true when calling admin-ajax.php, which is used when making AJAX calls from both front-end and back-end. This way it won't return true for AJAX calls – czerspalace Aug 31 '16 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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