string $hook_suffix )
Enqueue scripts for all admin pages.
The current admin page.
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.
View all references
do_action( 'admin_enqueue_scripts', $hook_suffix );
Selectively enqueue a script in the admin
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
Another way to load scripts or css in specific admin page by using this function
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.
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: