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'm going to be using a jQuery slider on my homepage only (not a WP plugin, but a third party). What I'd like to do is include jQuery, the plugin's jQuery script using a .js file, then include the script needed to fire the plugin right before the closing body tag. I'm a bit confused about how that works.

I know WordPress comes with jQuery built in, so does that mean I don't need to include it again?

Looking for best practices on this. I understand there are conflict issues that need to be prevented, etc.

Thanks

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

You typically should use the jQuery version that's built in. There are instances where you might want to use wp_dequeue_script('jquery') and then add a different version of jQuery.

Here's the best way to add a script dependent on jQuery:

function theme_register_scripts(){
   wp_enqueue_script('jquery');
   wp_enqueue_script('myscript-name', get_bloginfo('stylesheet_directory') . '/js/myscript.js', array('jquery'));
}
add_action('wp_print_scripts', 'theme_register_scripts');

The third argument of wp_enqueue_script is the script dependency which we identify as jQuery. This guarantees that jQuery will be printed before your custom script is printed.

share|improve this answer
    
So that handles including jQuery (using the built in WP version) and my jQuery plugin file. However, is there a specific best practice for firing the plugin? By the way, my firing script contains the jQuery "$". Is that an issue? Usually I would just include or add the firing script in my page template. Is there any reason not to do this? –  jw60660 Sep 25 '11 at 20:03
    
The reasoning for queuing up a script is for dependencies across the entire installations. This also helps you disable other scripts programatically without having to touch plugin or WP source code. For using $, read my Good, Bad and Ugly usage of jQuery in WP. coderrr.com/jquery-in-wordpress-the-good-the-bad-the-ugly –  Brian Fegter Sep 25 '11 at 21:46
    
Thanks very much; very useful. I learned a lot about implementing jQuery in WordPress. I read your blog post. More good advice. As an additional caution, after a few hours of scratching my head (and missing a good movie w the kids!), I learned the hard way that declaring the jQuery function directly must include the capital "Q" in jQuery to work. –  jw60660 Sep 25 '11 at 21:53
add comment

To add to that Brain said, you can use the enqueue_script function to add your js files to the footer if your theme uses wp_footer. Also when adding an action it is very useful when using javascript to use an order parameter so you can order them the way you wan't.

Something like:

wp_enqueue_script('myscript-name', 
                   get_bloginfo('stylesheet_directory') . '/js/myscript.js',
                   array('jquery'), 
                   'false',  //change version #
                   'true'); //in_footer?

//here we add a priority parameter to load one before the other if needed.
add_action('wp_print_scripts', 'theme_register_scripts_other', 10);
add_action('wp_print_scripts', 'theme_register_scripts', 20);   

You can also add it to the footer using wp_footer instead of wp_print_scripts

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

share|improve this answer
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.