I have two simple functions that load stuff using wp_enqueue_style()
and wp_enqueue_script()
, something like these:
function admin_custom_css()
{ wp_enqueue_style( 'stylesheet_name', 'stylesheet.css') };
function admin_custom_js
{ wp_enqueue_script( 'javascript_file', 'script.js') };
… and a few admin pages, created with add_menu_page()
and add_submenu_page()
function my_menu() {
add_menu_page('Page 1', 'bar', 'something', 'else', 'foo');
add_submenu_page( 'theme_menu', 'Subpage 1', 'Subpage', 'something', 'else', 'foo');
}
add_action('admin_menu', 'my_menu');
How do I load my two functions only on these pages?
Right now I’m using:
add_action('admin_init', 'admin_custom_css' );
add_action('admin_init', 'admin_custom_js' );
But it loads my files on every admin page, which is not nice at all.
Can I do this via one simple line in functions.php
or have to enqueue them within my pages separately (I prefer the first option strongly, since I’d have to edit a lot of admin-page-creating-functions).
Thanks!
add_menu_page
andadd_submenu_page
both return the page’s “hook suffix”, which can be used to identify the page with certain hooks. As such, you can use that suffix in combination with the variable hooksadmin_print_styles-{$hook_suffix}
andadmin_print_scripts-{$hook_suffix}
to specifically target these pages.I find this to be a clean method for adding all of this because it’s all handled within the one function. If you decide to remove this functionality, simply remove the call to the one function.
The problem with @tollmanz answer is that since you’re hooking off of the -print-styles and -print-scripts hooks, you must generate the HTML to load your scripts manually. This is not optimal, since you don’t get the nice dependency and versioning that comes with
wp_enqueue_script()
andwp_enqueue_style()
. It also doesn’t let you put things in the footer if that’s a better place for them.So, back to the OP’s question: what’s the best way to ensure that I can enqueue JS / CSS on specific admin pages only?
Hook off the “load-{$my_admin_page}” action to only do things when it’s your specific plugin’s admin page that’s loaded, and then
Hook off the “admin_enqueue_scripts” action to properly add your
wp_enqueue_script
calls.Seems like a bit of a pain, but it’s actually very easy to implement. From the top:
If you use
get_current_screen()
, you can detect what the page you’re on is. There is an example in the codex article that I linked which shows how to useget_current_screen()
withadd_options_page()
, this method will work for any admin page.I was wondering the same thing. There’s a modern version that uses
admin_enqueue_scripts
:As @mor7ifer mentioned above, you can use the native WordPress function get_current_screen(). If you loop through the output of this function, e.g.:
… you’ll notice a key called base. I use this detect what page I’m on and enqueue, dequeue like so:
To make it, you have to find the admin page name first. Add
admin_enqueue_scripts
withwp_die($hook)
and go to your specific plugin page, You will see the page name.Now copy the page name and use it in condition to load the scripts on the specific page.
You could take
@tollmanz
answer, and expand on it slightly, allowing for conditional usage as well…Example:
From the documentation:
Same with
admin_print_styles
.