Fires when enqueuing scripts for all admin pages.
Parameters
$hook_suffix
string- The current admin page.
Source
do_action( 'admin_enqueue_scripts', $hook_suffix );
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
Fires when enqueuing scripts for all admin pages.
$hook_suffix
stringdo_action( 'admin_enqueue_scripts', $hook_suffix );
Version | Description |
---|---|
2.8.0 | Introduced. |
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.
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.
Load css and js only on a particular sub-menu page
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 )
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:
Here’s how you can hook your namespaced function: