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'm trying to enqueue a JS script only when someone is adding or editing a custom post type I created called "recipes". Currently the script works ok when I do this:

if (is_admin()){
    wp_enqueue_script( 'my-script' );

But this loads it in every admin page, I'm assuming I need to hook it to a function but I hvae no idea what it'd be.

Thanks in advance!

share|improve this question
For future use, a similar question was answered with what I feel was an easier way: wordpress.stackexchange.com/q/34894/93169 – Frits Apr 29 at 6:37
up vote 24 down vote accepted

You can do it like this (put in your functions.php) :

function add_admin_scripts( $hook ) {

    global $post;

    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        if ( 'recipes' === $post->post_type ) {     
            wp_enqueue_script(  'myscript', get_stylesheet_directory_uri().'/js/myscript.js' );
add_action( 'admin_enqueue_scripts', 'add_admin_scripts', 10, 1 );
share|improve this answer
Can i also enqueue style using this function? – Sisir Nov 14 '11 at 11:16
@Sisir Use the "admin_print_styles-{$page}" hook. $page can hold the complete add_(sub)menu_page() code and be used to then target the page when using @see wp_enqueue_style(). – kaiser Nov 14 '11 at 18:04
A more up to date way of doing this: wordpress.stackexchange.com/a/34897/93169 (also happens to be a little easier thanks to functionality changes in wordpress...) – Frits Apr 29 at 6:38

There's a hook for that, and it's dead-simple to use. See this tutorial for an example implementation.


Justin moved his tutorials from DevPress to his personal site. Here's the updated link for the tutorial.

share|improve this answer
The page listed is gone. Here's a mirror/archive on the Wayback machine. – Robert K Mar 8 '12 at 21:42
Thanks for that. I've updated my answer with a current link. – Chip Bennett Mar 8 '12 at 22:04
Why not post the solution here and link the tutorial as the source... – Christine Cooper Nov 8 '14 at 16:33

Rootstheme (which is based on Twitter Bootstrap) has a really elegant way of loading scripts depending on the page/post type as seen in the roots_scripts function which can be seen here on github.

Basically register all your scripts and styles then have conditional statements that wrap your wp_enqueue_script or wp_enqueue_style statements.

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.