admin_print_scripts-(hook_suffix)
Fires when scripts are printed on a particular admin page.
It is used when specific scripts should be output only on selected admin pages.
It allows JavaScript code or files to be added to the admin <head> based on the hook_suffix value, which identifies a particular admin page, such as post.php or toplevel_page_myplugin.
This event fires after admin_enqueue_scripts, but it is used less often because admin_enqueue_scripts with a $hook_suffix parameter check is normally preferred.
Use load-(pagenow) to perform an action while a core page loads.
Use load-(page_hook) to perform an action while a plugin page (a custom registered page) loads.
Usage
add_action( 'admin_print_scripts-(hook_suffix)', 'wp_kama_admin_print_scripts_hook_suffix_action' );
/**
* Function for `admin_print_scripts-(hook_suffix)` action-hook.
*
* @return void
*/
function wp_kama_admin_print_scripts_hook_suffix_action(){
// action...
}
- $hook_suffix(string)
Unique identifier of the current admin page. Used in the hook name.
Depending on the page type,
$hook_suffixmay be the value of one of these global variables:$hook_suffix = $page_hook; // Result of add_menu_page() or add_submenu_page(). // Or: $hook_suffix = $plugin_page; // plugin_basename( $_GET['page'] ). // Or: $hook_suffix = $pagenow; // PHP filename.
See wp-admin/admin.php.
Possible values of the dynamic
$hook_suffixparameter:For $pagenow
index.php— main Dashboard page.edit.php— posts list.post.php— edit a post.post-new.php— create a new post.edit-tags.php— manage tags and categories.upload.php— Media Library.media-new.php— upload a new media file.edit-comments.php— comments.themes.php— themes.customize.php— Customizer.widgets.php— widgets.nav-menus.php— navigation menus.plugins.php— plugins list.plugin-install.php— install plugins.users.php— users list.user-new.php— add a new user.profile.php— current user profile.edit.php— pages list.edit.php— custom post type list.options-general.php— General Settings.options-writing.php— Writing Settings.options-reading.php— Reading Settings.options-discussion.php— Discussion Settings.options-media.php— Media Settings.options-permalink.php— Permalink Settings.tools.php— Tools.import.php— Import.export.php— Export.admin.php— plugin and custom pages.
For $page_hook
A unique$page_hookis created for custom pages added with add_menu_page() or add_submenu_page(). It is returned by the registration functions and looks like:toplevel_page_mypluginmyplugin_page_settings.
Examples
#1 Enqueue a script only on a plugin settings page
Add a script only to a settings page created with add_submenu_page().
add_action( 'admin_menu', 'my_plugin_admin_menu' );
function my_plugin_admin_menu() {
$hook = add_submenu_page(
'options-general.php',
'My Plugin Settings',
'My Plugin',
'manage_options',
'my-plugin',
'my_plugin_settings_page'
);
add_action( "admin_print_scripts-{$hook}", 'my_plugin_admin_scripts' );
}
function my_plugin_admin_scripts() {
wp_enqueue_script( 'my-plugin-script', plugins_url( 'script.js', __FILE__ ) );
}Changelog
| Since 2.1.0 | Introduced. |
Where the hook is called
do_action( "admin_print_scripts-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
do_action( "admin_print_scripts-{$hook_suffix}" ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
Where the hook is used in WordPress
add_action( 'admin_print_scripts-index.php', 'wp_localize_community_events' );
add_action( 'admin_print_scripts-post.php', 'wp_page_reload_on_back_button_js' );
add_action( 'admin_print_scripts-post-new.php', 'wp_page_reload_on_back_button_js' );
add_action( "admin_print_scripts-{$page}", array( $this, 'js_includes' ) );
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );