WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For a WP Theme with multiple admin screen (got its own main menu and some submenus), I want to add a common Js script to load on (only) all these pages.

I know how to acheive this using the pagehooks of the individual (sub)pages lioke this:

add_action('admin_print_scripts-' . $page, 'my_plugin_admin_script');

but this way I need to reåeat this for each admin page (for each submenu pagehook)

Is there a way smarter way to add the scripts, testing for the PARENT menu? so that I just need to add it with one line of admin_print_scripts-* code?

share|improve this question
up vote 2 down vote accepted

It would be easiest to run some conditional logic on $parent_file inside a callback hooked onto admin_print_scripts, and would go a little something like this..

add_action( 'admin_print_scripts', 'possibly_enqueue_script' );
function possibly_enqueue_script() {
    global $parent_file;
    if( 'my-slug' == $parent_file )
        wp_enqueue_script(  ... your enqueue args .. );

You'll need to replace my-slug with the handle of your parent page, it's the fourth parameter in add_menu_page...

The script will then enqueue for both the parent page and any of it's children pages..

Hope that helps...

share|improve this answer
That's the same thing I did for one of my own plugins, well it's similar. – Not Available Dec 20 '10 at 14:27
It should be reliable to, as far as i can tell it's set for every page you see in the admin area. The alternative would be some sort of loop that registers the plugin pages, and adds appropriate actions for each iteration(but it wouldn't be as elegant as the above imho). – t31os Dec 20 '10 at 15:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.