How do I Enqueue styles/scripts on Certain /wp-admin Pages?

I have two simple functions that load stuff using wp_enqueue_style() and wp_enqueue_script(), something like these:

function admin_custom_css()
{ wp_enqueue_style( 'stylesheet_name', 'stylesheet.css') }; 

function admin_custom_js 
{ wp_enqueue_script( 'javascript_file', 'script.js') };

… and a few admin pages, created with add_menu_page() and add_submenu_page()

Read More
function my_menu() {
   add_menu_page('Page 1', 'bar', 'something', 'else', 'foo');
   add_submenu_page( 'theme_menu', 'Subpage 1', 'Subpage', 'something', 'else', 'foo'); 
}
add_action('admin_menu', 'my_menu'); 

How do I load my two functions only on these pages?

Right now I’m using:

add_action('admin_init', 'admin_custom_css' ); 
add_action('admin_init', 'admin_custom_js' );  

But it loads my files on every admin page, which is not nice at all.

Can I do this via one simple line in functions.php or have to enqueue them within my pages separately (I prefer the first option strongly, since I’d have to edit a lot of admin-page-creating-functions).

Thanks!

Related posts

Leave a Reply

9 comments

  1. add_menu_page and add_submenu_page both return the page’s “hook suffix”, which can be used to identify the page with certain hooks. As such, you can use that suffix in combination with the variable hooks admin_print_styles-{$hook_suffix} and admin_print_scripts-{$hook_suffix} to specifically target these pages.

    function my_menu() {
       $menu = add_menu_page( 'Page 1', 'bar', 'something', 'else', 'foo' );
       $submenu = add_submenu_page( 'theme_menu', 'Subpage 1', 'Subpage', 'something', 'else', 'foo' );
    
       add_action( 'admin_print_styles-' . $menu, 'admin_custom_css' );
       add_action( 'admin_print_styles-' . $submenu, 'admin_custom_css' );
    
       add_action( 'admin_print_scripts-' . $menu, 'admin_custom_js' );
       add_action( 'admin_print_scripts-' . $submenu, 'admin_custom_js' );
    }
    

    I find this to be a clean method for adding all of this because it’s all handled within the one function. If you decide to remove this functionality, simply remove the call to the one function.

  2. The problem with @tollmanz answer is that since you’re hooking off of the -print-styles and -print-scripts hooks, you must generate the HTML to load your scripts manually. This is not optimal, since you don’t get the nice dependency and versioning that comes with wp_enqueue_script() and wp_enqueue_style(). It also doesn’t let you put things in the footer if that’s a better place for them.

    So, back to the OP’s question: what’s the best way to ensure that I can enqueue JS / CSS on specific admin pages only?

    1. Hook off the “load-{$my_admin_page}” action to only do things when it’s your specific plugin’s admin page that’s loaded, and then

    2. Hook off the “admin_enqueue_scripts” action to properly add your wp_enqueue_script calls.

    Seems like a bit of a pain, but it’s actually very easy to implement. From the top:

        add_action( 'admin_menu', 'add_my_admin_menus' ); // hook so we can add menus to our admin left-hand menu
    
        /**
         * Create the administration menus in the left-hand nav and load the JavaScript conditionally only on that page
         */
        function add_my_admin_menus(){
            $my_page = add_menu_page( 'Page Title', 'Menu Title', MY_ADMIN_CAPABILITY, 'menu-slug', 'show_page_content' );
    
            // Load the JS conditionally
            add_action( 'load-' . $my_page, 'load_admin_js' );
        }
    
        // This function is only called when our plugin's page loads!
        function load_admin_js(){
            // Unfortunately we can't just enqueue our scripts here - it's too early. So register against the proper action hook to do it
            add_action( 'admin_enqueue_scripts', 'enqueue_admin_js' );
        }
    
        function enqueue_admin_js(){
            // Isn't it nice to use dependencies and the already registered core js files?
            wp_enqueue_script( 'my-script', INCLUDES_URI . '/js/my_script.js', array( 'jquery-ui-core', 'jquery-ui-tabs' ) );
        }
    }
    
  3. If you use get_current_screen(), you can detect what the page you’re on is. There is an example in the codex article that I linked which shows how to use get_current_screen() with add_options_page(), this method will work for any admin page.

  4. I was wondering the same thing. There’s a modern version that uses admin_enqueue_scripts:

    add_action('admin_menu', function () {
      $settingsPage = add_options_page('Settings', 'Mortgage Calculator', 'manage_options', 'mortgagecalculator', function () {
        echo "<div id='app'></div>";
      });
      /**
       * Include the ember admin scripts only on pages where it's needed.
       */
      add_action("admin_enqueue_scripts", function ($hook) use ($settingsPage){
        if($hook !== $settingsPage){
          return;
        }
        // Remove default jQuery since Ember provides its own.
        wp_dequeue_script('jquery');
        wp_enqueue_script('ember-vendor', plugins_url("admin/assets/vendor.js", __FILE__));
        wp_enqueue_script('ember-project', plugins_url("admin/assets/mortgage-plugin-ember-admin.js", __FILE__), ['ember-vendor']);
        wp_enqueue_script('ember-live-reload', "http://localhost:4200/ember-cli-live-reload.js");
      });
    });
    
  5. As @mor7ifer mentioned above, you can use the native WordPress function get_current_screen(). If you loop through the output of this function, e.g.:

    $current_screen = get_current_screen();
    foreach($current_screen as $key => $value) {
        error_log(print_r($key,1));
    }
    

    … you’ll notice a key called base. I use this detect what page I’m on and enqueue, dequeue like so:

    add_action('admin_enqueue_scripts', 'some_hook_function')* ):
    
    public function some_hook_function(){
    
        // # only register and queue scripts & styles on POST edit screen of admin
        $current_page = get_current_screen()->base;
        if($current_page == 'post' || $current_page == 'page') {
    
            wp_enqueue_script('datetimepicker', plugins_url('assets/jquery-ui-timepicker-addon.min.js', __FILE__), array('jquery', 'jquery-ui-datepicker', 'jquery-ui-slider'), '1.9.1', true);
    
            wp_enqueue_style( 'jquery-ui-datepicker', plugins_url('assets/jquery-ui.min.css', __FILE__), array(), '1.11.2', 'all');
    
    
        } else { // # if not on post page, deregister and dequeue styles & scripts
    
            wp_dequeue_script('datetimepicker');
            wp_dequeue_style('jquery-ui-datepicker');
    
        }
    }
    
  6. To make it, you have to find the admin page name first. Add admin_enqueue_scripts with wp_die($hook) and go to your specific plugin page, You will see the page name.

    function my_plugin_scripts($hook) {
        wp_die($hook);
    }
    add_action( 'admin_enqueue_scripts', 'my_plugin_scripts' );
    

    settings_page_plugging_info

    Now copy the page name and use it in condition to load the scripts on the specific page.

    function my_plugin_scripts($hook) {
        if ( 'settings_page_plugging_info' != $hook ) {
            return;
        }
    
        wp_enqueue_script( 'my_custom_script', plugins_url('js/file.js', __FILE__));
    }
    add_action( 'admin_enqueue_scripts', 'my_plugin_scripts' );
    
  7. You could take @tollmanz answer, and expand on it slightly, allowing for conditional usage as well…

    Example:

    /* Add admin pages */   
    function my_admin_pages(){
        $menu = array();
        $menu['main_page'] = add_menu_page('Page 1', 'bar', 'something', 'else', 'foo');
        $menu['sub_page'] = add_submenu_page('theme_menu', 'Subpage 1', 'Subpage', 'something', 'else', 'foo');
        foreach($menu as $key => $value){
            if($key == 'main_page'){
                /* Print styles on only the main page. */
                add_action('admin_print_styles-'.$value, 'print_styles');
            }
            /* Print scripts on all of our admin pages. */
            add_action('admin_print_scripts-'.$value, 'print_scripts');
        }
    }
    add_action('admin_menu', 'my_admin_pages');
    
  8. add_action( 'admin_menu', 'add_my_admin_menus' ); 
    
    function add_my_admin_menus() {
        $GLOBALS['my_page'] = add_menu_page( 'Page Title', 'Menu Title', MY_ADMIN_CAPABILITY, 'menu-slug', 'show_page_content');
        add_action( 'admin_enqueue_scripts', 'enqueue_admin_js');
    }
    
    function enqueue_admin_js($hook) {
        if($GLOBALS['my_page'] === $hook) {
            wp_enqueue_script( 'jquery-ui-core' );
            wp_enqueue_script( 'jquery-ui-tabs' );
            // Isn't it nice to use dependencies and the already registered core js files?
            wp_enqueue_script( 'my-script', INCLUDES_URI . '/js/my_script.js', array( 'jquery-ui-core', 'jquery-ui-tabs' ) );
        }
    }