0

This makes the Jquery script work in a plugin:

wp_enqueue_script('metabox_js', plugins_url('add_meta_box/js/metabox.js',dirname(__FILE__) ), array('jquery'));

I have been trying for ages, & this doesn't make the script work in a theme (have put this in functions.php):

function prefix_enqueue_scripts3() {
    wp_enqueue_script( 'metabox_js', get_stylesheet_directory_uri() . '/js/metabox.js', array( 'jquery' ), '1.0.0', true );
                  }  

add_action( 'wp_enqueue_scripts','prefix_enqueue_scripts3'); 

Please can someone advise. I have another Jquery file further up functions.php, which uses Jquery successfully, I have only changed the file location and file name with this one. Please can someone advise? Thank-you!

  • This looks correct. Does the metabox not load? One thing to note is that in prefix_enqueue_scripts3 you are setting the script to load in the footer, not sure if that might be causing your issue. – czerspalace Jan 15 '15 at 20:22
  • The metabox is shown in the add new post area. This Jquery file is suppose to hide the metabox and only show when a category is selected. I'm trying to make this work for the add new post editor area. Could loading in the footer cause this issue? How should I do it please? Thank-you! – Leona grey Jan 15 '15 at 20:25
  • I changed true to false and it didn't make any difference – Leona grey Jan 15 '15 at 20:37
0

From wp_enqueue_scripts action:

wp_enqueue_scripts is the proper hook to use when enqueuing items that are meant to appear on the front end.

For admin-facing scripts, the correct action hook is admin_enqueue_scripts

function prefix_enqueue_scripts3() {
    wp_enqueue_script(
        'metabox_js',
        get_stylesheet_directory_uri() . '/js/metabox.js',
        array( 'jquery' ),
        '1.0.0',
        true
    );
}
add_action( 'admin_enqueue_scripts', 'prefix_enqueue_scripts3' ); 
  • Thank-you, that doesn't seem to have solved my problem either though. – Leona grey Jan 15 '15 at 20:58
  • I looked at the Jquery file, if I list more than one category to show the metabox for it works, if I add one it does not. I'll have to work that out. Thank-you for the help Milo! – Leona grey Jan 15 '15 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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