do_action( 'admin_enqueue_scripts', string $hook_suffix )
Enqueue scripts for all admin pages.
Parameters
-
$hook_suffix
string -
The current admin page.
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.
Source
File: wp-admin/admin-header.php
.
View all references
do_action( 'admin_enqueue_scripts', $hook_suffix );
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
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.
Figure out your $hook name
If you are unsure what the $hook name of the current admin page of which you want to conditionally load your script is, add this to your page:
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:
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.
Load css and js only on a particular sub-menu page
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:
Load your scripts on your menu page and all sub-menu below your menu page.
What if you want to load CSS, JS to specific pages from your created menu and submenu? ( multiple pages )
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:
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.
Here’s how you can hook your namespaced function: