do_action( 'admin_enqueue_scripts', string $hook_suffix )

Enqueue scripts for all admin pages.


Parameters

$hook_suffix string
The current admin page.

Top ↑

More Information

admin_enqueue_scripts is the proper hook to use when enqueuing scripts and styles that are meant to be used in the administration panel. Despite the name, it is used for enqueuing both scripts and styles.

It provides a single parameter, $hook_suffix, that informs the current admin page. This should be used to enqueue scripts and styles only in the pages they are going to be used, and avoid adding script and styles to all admin dashboard unnecessarily.


Top ↑

Source

File: wp-admin/admin-header.php. View all references

do_action( 'admin_enqueue_scripts', $hook_suffix );


Top ↑

Changelog

Changelog
Version Description
2.8.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Drew Jaynes

    Selectively enqueue a script in the admin

    The admin_enqueue_scripts action hook can also be used to target a specific admin page. In this example we are loading a javascript file in the head section of edit.php.

    /**
     * Enqueue a script in the WordPress admin on edit.php.
     *
     * @param int $hook Hook suffix for the current admin page.
     */
    function wpdocs_selectively_enqueue_admin_script( $hook ) {
        if ( 'edit.php' != $hook ) {
            return;
        }
        wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'myscript.js', array(), '1.0' );
    }
    add_action( 'admin_enqueue_scripts', 'wpdocs_selectively_enqueue_admin_script' );
  2. Skip to note 3 content
    Contributed by Drew Jaynes

    Enqueue a custom stylesheet in the admin

    Sometimes you want to load a set of CSS and/or Javascript documents to all admin pages. You can do this from within your plugin or from your themes function file:

    /**
     * Register and enqueue a custom stylesheet in the WordPress admin.
     */
    function wpdocs_enqueue_custom_admin_style() {
            wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
            wp_enqueue_style( 'custom_wp_admin_css' );
    }
    add_action( 'admin_enqueue_scripts', 'wpdocs_enqueue_custom_admin_style' );
  3. Skip to note 4 content
    Contributed by itskawsar

    Another way to load scripts or css in specific admin page by using this function

    In this example, we are loading a javascript and a css file in the head section of nav-menus.php page.

    function add_script_to_menu_page()
    {
    	// $pagenow, is a global variable referring to the filename of the current page, 
    	// such as ‘admin.php’, ‘post-new.php’
    	global $pagenow;
    
    	if ($pagenow != 'nav-menus.php') {
    		return;
    	}
    	
    	// loading css
        wp_register_style( 'some-css', get_template_directory_uri() . '/css/some.css', false, '1.0.0' );
        wp_enqueue_style( 'some-css' );
    	
    	// loading js
    	wp_register_script( 'some-js', get_template_directory_uri().'/js/some.js', array('jquery-core'), false, true );
        wp_enqueue_script( 'some-js' );
    }
    
    add_action( 'admin_enqueue_scripts', 'add_script_to_menu_page' );
  4. Skip to note 5 content
    Contributed by pilate

    The $hook_suffix is kind of tricky to know it’s value out of your head especially on custom admin pages. This junky little trick can help:

    function wpdocs_myselective_css_or_js( $hook ) {
        echo '<h1 style="color: crimson;">' . esc_html( $hook ) . '</h1>';
    }
    
    add_action( 'admin_enqueue_scripts', 'wpdocs_myselective_css_or_js' ); 
  5. Skip to note 6 content
    Contributed by Anonymous User

    Load css and js only on a particular sub-menu page

    // custom css and js
    add_action('admin_enqueue_scripts', 'cstm_css_and_js');
    
    function cstm_css_and_js($hook) {
        // your-slug =&gt; The slug name to refer to this menu used in &quot;add_submenu_page&quot;
    		// tools_page =&gt; refers to Tools top menu, so it's a Tools' sub-menu page
        if ( 'tools_page_your-slug' != $hook ) {
            return;
        }
    
        wp_enqueue_style('boot_css', plugins_url('inc/bootstrap.css',__FILE__ ));
        wp_enqueue_script('boot_js', plugins_url('inc/bootstrap.js',__FILE__ ));
    }
  6. Skip to note 7 content
    Contributed by Dhimas Kirana

    Load your scripts on your menu page and all sub-menu below your menu page.

    /** Add Admin Pages **/
    function add_admin_pages() {
    
        // Add Menu Page
        add_menu_page('Top Level Menu', 'Top Level Menu', 'manage_options', 'top-level-menu', 'your_callback_menu', 'dashicons-admin-tools', 10);
    
        // Add Sub Menu Page
        add_submenu_page('top-level-menu', 'Sub Menu', 'Sub Menu', 'manage_options', 'sub-menu', 'your_callback_submenu');
    
    }
    add_action('admin_menu', 'add_admin_pages');
    
    /** Enqueue Stylesheets **/
    function add_admin_scripts($hook) {
    
        // global $parent_file defined on admin-header.php line 27
        // https://core.trac.wordpress.org/browser/tags/5.9/src/wp-admin/admin-header.php#L27
        global $parent_file;
        if ('top-level-menu' != $parent_file) {
            return;
        }
    
        // For example we load sweetalert on your menu page and all sub-menu below your menu page.
        wp_enqueue_script('sweetalert-js', 'https://unpkg.com/sweetalert/dist/sweetalert.min.js');
    
    }
    add_action('admin_enqueue_scripts', 'add_admin_scripts');
  7. Skip to note 8 content
    Contributed by Mahbub Hasan Imon

    What if you want to load CSS, JS to specific pages from your created menu and submenu? ( multiple pages )

    function addPage()
    {
    global $customMenu, $customSubMenu;
            /**
             * Menu
             */
           $customMenu = add_menu_page( 'Custom Menu', 'Custom Menu', 'manage_options', 'custom-menu', 'customMenuPage', '', 10);
            /**
             * Sub Menu Pages
             */
            $customSubMenu = add_submenu_page( 'custom-menu', 'Settings', 'Settings', 'manage_options', 'settings', 'settings_page');
    }
    add_action( 'admin_menu', 'addPage');
      
    /** Enqueue Stylesheets **/
    function enqueueAdminStyles( $hook)
        {
            global $customMenu, $customSubMenu;
            $allowed = array( $customMenu, $customSubMenu);
            if( !in_array( $hook, $allowed)  )
            {
                return;
            }
            wp_enqueue_style( '-main-', 'assets/admin/css/ucsi.css', '', '1');
        }
    add_action( 'admin_enqueue_scripts', 'enqueueAdminStyles');
  8. Skip to note 9 content
    Contributed by Matt Keys

    Note: if you are trying to use the $hook_suffix to check if you are on a submenu page, there is an important bug you should know about. This mostly affects people who are distributing their code in a theme or plugin, where the code will be run on WordPress installations in multiple languages.

    https://core.trac.wordpress.org/ticket/18857

    Basically the part of the $hook_suffix that is the parent menu page slug can be translated, so it will not match the string you are expecting.

    You can work around this bug using code like:

    function enqueue_my_assets( $hook_suffix ) {
    	$parent_menu_slug = sanitize_title( __( 'Parent Menu Title', 'parent-translation-domain' ) );
    
    	if ( $parent_menu_slug . '_page_my_sub_menu' != $hook_suffix ) {
    		return;
    	}
    
    	// we must be on the right page then ...
    }
    add_action( 'admin_enqueue_scripts', 'enqueue_my_assets', 10, 1 );

    Basically you get the translated parent menu slug first, and use it when checking the $hook_suffix to make sure you are on the right page.

  9. Skip to note 10 content
    Contributed by Chigozie Orunta

    Here’s how you can hook your namespaced function:

    namespace YourNameSpace;
    
    function register_your_admin_script() {
    	wp_enqueue_script( 'your-admin-script', get_template_directory_uri() . '/js/your-admin-script.js', array(), '1.0.0', true );
    }
    
    add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\register_your_admin_script' );

You must log in before being able to contribute a note or feedback.