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

I have some script that is loading in the admin. I only need it to load on the new post and edit posts screens.

What is the best way to do this?

share|improve this question
up vote 2 down vote accepted

Check the page and enqueue your script accordingly:

global $pagenow;
if (! empty($pagenow) && ('post-new.php' === $pagenow || 'post.php' === $pagenow ))
    add_action('admin_enqueue_scripts', 'enqueue_my_scripts');

function enqueue_my_scripts() {
} // function enqueue_my_scripts
share|improve this answer
I'd personally have put the conditional logic into the callback, aside from that i agree with the choice of hook. :) – t31os Apr 25 '13 at 17:10
@t31os Why make the parser travel further than necessary? ;) And also, you'd add an action that does not do anything for most of the pages/time. – tfrommen Apr 25 '13 at 17:12
Correct, but it looks prettier. The runtime difference should be pretty negligible to. – t31os Apr 25 '13 at 17:16
Chosen as the answer. Though the one above seems like it would work as well. I ended up figuring this out on my own but what you suggested is basically what I did. – dcp3450 Apr 30 '13 at 19:23

The global variable $hook_suffix is:

  • post-new.php for the new post and
  • post.php for the regular post editor

In wp-admin/admin-header.php there are some special hooks:

do_action('admin_enqueue_scripts', $hook_suffix);

So you can use admin_print_styles-$hook_suffix, in your cases:

add_action( 'admin_print_scripts-post-new.php', 'your_prefix_enqueue_scripts' );
add_action( 'admin_print_scripts-post.php',     'your_prefix_enqueue_scripts' );

To find the correct $hook_suffix for any admin page use something like this:

add_action( 'admin_footer', 'print_hook_suffix' );

function print_hook_suffix()
    global $hook_suffix;

    print '$hook_suffix: ' . $hook_suffix;
share|improve this answer

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.