57

I'm building a plugin and I want to add bits of javascript in the admin head but only for certain admin pages. I don't mean pages as in a WordPress page that you create yourself but rather existing admin section pages like 'Your Profile', 'Users', etc. Is there a wp function specifically for this task? I've been looking and I can only find the boolean function is_admin and action hooks but not a boolean function that just checks.

1
  • 1
    Gentle nudge to remind you an answer has not been accepted. If none the answers provided answer your question sufficiently or you're struggling to understand the information provided please comment to let us know.
    – t31os
    Commented Feb 24, 2011 at 17:21

5 Answers 5

32

The way to do this is to use the 'admin_enqueue_scripts' hook to en-queue the files you need. This hook will get passed a $hook_suffix that relates to the current page that is loaded:

function my_admin_enqueue($hook_suffix) {
    if($hook_suffix == 'appearance_page_theme-options') {
        wp_enqueue_script('my-theme-settings', get_template_directory_uri() . '/js/theme-settings.js', array('jquery'));
        wp_enqueue_style('my-theme-settings', get_template_directory_uri() . '/styles/theme-settings.css');
        ?>
        <script type="text/javascript">
        //<![CDATA[
        var template_directory = '<?php echo get_template_directory_uri() ?>';
        //]]>
        </script>
        <?php
    }
}
add_action('admin_enqueue_scripts', 'my_admin_enqueue');
0
86

There is a global variable in wp-admin called $pagenow which holds name of the current page, ie edit.php, post.php, etc.

You can also check the $_GET request to narrow your location down further, for example:

global $pagenow;
if (( $pagenow == 'post.php' ) && ($_GET['post_type'] == 'page')) {

    // editing a page

    }

if ($pagenow == 'users.php') {

    // user listing page

    }

if ($pagenow == 'profile.php') {

    // editing user profile page

    }
3
  • 3
    global $pagenow; if ( ( 'admin.php' === $pagenow ) && ( 'prefix-theme-settings' === $_GET['page'] ) ) { logic.. } for example checks if you are on a custom theme settings page admin.php?page=prefix-theme-settings.
    – lowtechsun
    Commented Feb 14, 2018 at 21:01
  • thank you . This is a great answer and helped it. Commented Nov 5, 2021 at 11:06
  • This is perfect. Thanks a lot! I tried the $wp global variable, but the request property would remain empty there. Commented Feb 2, 2023 at 13:29
35

The most comprehensive method is get_current_screen added in WordPress 3.1

$screen = get_current_screen();

returns

WP_Screen Object (
    [action] => 
    [base] => post
    [id] => post
    [is_network] => 
    [is_user] => 
    [parent_base] => edit
    [parent_file] => edit.php
    [post_type] => post
    [taxonomy] => 
)
5
  • 6
    "Usage Restrictions -- This function is defined on most admin pages, but not all. Thus there are cases where is_admin() will return true, but attempting to call get_current_screen() will result in a fatal error because it is not defined. One known example is wp-admin/customize.php. The function returns null if called from the admin_init hook. It should be OK to use in a later hook such as current_screen. " -- Codex. Commented Jan 26, 2018 at 16:17
  • @ThatBrazilianGuy A quick check suggests this is no longer the case, as far as I can see get_current_screen() is safe to use on all admin pages.
    – Steven
    Commented Jan 27, 2019 at 8:31
  • 2
    @shahar this is still the case, according to the documentation linked above and my experience.
    – fabrik
    Commented Mar 14, 2019 at 8:37
  • it is still the case. You can reproduce it when creating a plugin and then activating it. This will throw a fatal error on activation since you are on a page where the function is not defined :(
    – seawave_23
    Commented Jan 4, 2021 at 14:34
  • you can always check&bail if (!function_exists('get_current_screen')) return ; if your logic depends on it :)
    – jave.web
    Commented Feb 26, 2021 at 1:41
11

To offer an alternative method/approach to the above question.

// When you are viewing the users list or your editing another user's profile
add_action( 'admin_print_scripts-users.php', 'your_enqueue_callback' );

// When you are editing your own profile
add_action( 'admin_print_scripts-profile.php', 'your_enqueue_callback' );

function your_enqueue_callback() {
    wp_enqueue_script( .. YOUR ENQUEUE ARGS .. );
}

This method targets the specific pages more directly and avoids needing conditional logic inside your callback(because you've already made that distinction in the selected hook).

1
  • Wish i knew why this answer didn't get more votes, covers the exact use case in the question, and provides the solution in a minimal amount of code. Feedback would be nice..
    – t31os
    Commented Jan 28, 2014 at 14:56
8

I find it weird that no one has mentioned the fact that the add_menu_page function returns an action hook which you can use to do certain actions only on those pages

$hook = add_menu_page($menu_title, $page_title, $capability, $slug, $function, $icon_url, $position);
add_action( 'load-' . $hook, 'my_admin_enqueue_scripts' );
function my_admin_enqueue_scripts() {
    wp_enqueue_script(/*...*/);
    wp_enqueue_style(/*...*/);
}

If you need the $hook and you didn't add the menu page yourself the doc is here

For instance the hook for a top level menu page is

load-toplevel_page_$MenuSlug

The hook for a submenu page is

load-$MenuSlug_page_$SubMenuSlug

Following that logic, the hook for the user's profile page is

load-users_page_profile

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.