2

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' );

3 Answers 3

3

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' );
4
  • 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, 2013 at 8:00
  • I've edited code see if it's working
    – JMau
    Sep 20, 2013 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, 2013 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. Sep 20, 2013 at 8:45
0

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!!

2
  • 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, 2013 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 Sep 20, 2013 at 8:43
-2

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' );
2
  • hook to enqueue stylesheets & scripts in admin is admin_enqueue_scripts
    – JMau
    Sep 24, 2013 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.
    – cybmeta
    Sep 24, 2013 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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