Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have added a few meta boxes for the posts and pages, so I want to load a js script only on the Create/Edit post and page. How can I do that? Currently I'm using following but that loads the scripts in all pages in the admin.

function my_meta_boxes() {
    add_meta_box("post_meta", "Meta", "post_meta", "post", "normal", "high");
    add_meta_box("post_meta", "Meta", "post_meta", "page", "normal", "high");

}
add_action( 'add_meta_boxes', 'my_meta_boxes' );

function register_custom_scripts() {
    wp_enqueue_script( 'custom_js', get_template_directory_uri() . '/inc/meta/custom.js', array( 'jquery' ));
    wp_enqueue_style( 'custom_css', get_template_directory_uri() . '/inc/meta/custom.css');
}
add_action( 'admin_init', 'register_custom_scripts' );
share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

We can improve a little bit with :

function specific_enqueue($hook_suffix) {
   if( 'post.php' == $hook_suffix || 'post-new.php' == $hook_suffix ) {
     wp_enqueue_script( 'custom_js', get_template_directory_uri() . '/inc/meta/custom.js', array( 'jquery' ));
     wp_enqueue_style( 'custom_css', get_template_directory_uri() . '/inc/meta/custom.css')
  }
}
add_action( 'admin_enqueue_scripts', 'specific_enqueue' );
share|improve this answer
    
Thanks, but I have same problem, I get NULL value for $hook_suffix, thats why the condition doesn't become true and it doesn't load the scripts. I must be doing something wrong. Do I need to define $hook_suffix somewhere? –  jay Sep 20 '13 at 8:00
    
I've edited code see if it's working –  JMau Sep 20 '13 at 8:08
    
Oh, my mistake. I didn't realize that I was using admin_init instead of admin_enqueue_scripts. Thanks again. –  jay Sep 20 '13 at 8:40
    
@jay yes you need to use admin_enqueue_scripts, I have mentioned it in the first line of my answer. Glad that you got the solution. –  Maruti Mohanty Sep 20 '13 at 8:45
add comment

Use admin_enqueue_scripts hook to enqueue scripts to the admin pages and to make it to specific page follow the below:--

function specific_enqueue( $hook ) {
    if( 'post.php' != $hook )
        return;
    wp_enqueue_script( 'custom_js', get_template_directory_uri() . '/inc/meta/custom.js', array( 'jquery' ));
    wp_enqueue_style( 'custom_css', get_template_directory_uri() . '/inc/meta/custom.css')
}
add_action( 'admin_enqueue_scripts', 'specific_enqueue' );

Hope it helps!!

share|improve this answer
    
Thanks for the answer but it does not seem to work. I checked var_dump($hook); and I get empty string. Do I need to define $hook somewhere? –  jay Sep 20 '13 at 7:20
    
Both the answers should work. and when you var_dump it you should see the file it is rendering. Edited the answer Please check –  Maruti Mohanty Sep 20 '13 at 8:43
add comment

I have another approach which requires less code. As you are adding the scripts to work with metaboxes, you can equeue the scripts in the add_meta_boxes() action hook. It works perfectly and you don't need to check the actual page in every request for the admin area.

function my_meta_boxes() {

    add_meta_box("post_meta", "Meta", "post_meta", "post", "normal", "high");
    add_meta_box("post_meta", "Meta", "post_meta", "page", "normal", "high");
    wp_enqueue_script( 'custom_js', get_template_directory_uri() . '/inc/meta/custom.js', array( 'jquery' ));
    wp_enqueue_style( 'custom_css', get_template_directory_uri() . '/inc/meta/custom.css');

}
add_action( 'add_meta_boxes', 'my_meta_boxes' );
share|improve this answer
    
hook to enqueue stylesheets & scripts in admin is admin_enqueue_scripts –  JMau Sep 24 '13 at 12:50
    
Thats is not always needed and it is not always de best option. Although it depends on the situation, the purpose of the plugin (private/commercial) and the person who maintain the website. But, these words can be in the "opinion based" side. –  cybnet Sep 24 '13 at 14:47
add comment

Your Answer

 
discard

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.