admin_enqueue_scripts
Loads stylesheets and scripts in the WordPress admin. Its $hook_suffix parameter tells you which admin screen is loading.
When it fires
admin_enqueue_scripts runs on every admin page while the admin header is being built, before the styles and scripts in the are printed. The current user and screen are already set up.
Use $hook_suffix to load files only where they're needed. It's 'index.php' on the Dashboard, 'edit.php' on post lists, 'post.php' and 'post-new.php' in the editor, and values like 'toplevel_page_my-page' or 'settings_page_my-page' for pages added with add_menu_page() or add_options_page().
Parameters
$hook_suffixstring |
The current admin page. |
|---|
Example: Add CSS to the posts list only
add_action( 'admin_enqueue_scripts', function ( $hook_suffix ) {
if ( 'edit.php' !== $hook_suffix ) {
return;
}
wp_register_style( 'my-posts-list', false, array(), '1.0.0' );
wp_enqueue_style( 'my-posts-list' );
wp_add_inline_style( 'my-posts-list', '.fixed .column-title { width: 40%; }' );
} );
Good to know
- Not sure of the value on a screen? Temporarily log it with error_log( $hook_suffix ) and reload the page.
- The block editor's content area is usually an iframe, so admin CSS may not reach it. Use enqueue_block_assets for styles that affect post content.