1

I am pretty new in WordPress and I have the following doubt: I have to include JQuery into a theme and I am doing in this way:

I create the following function into functions.php theme file and I add it as action:

function load_java_scripts() {
    // Load FlexSlider JavaScript that handle the SlideShow:
    wp_enqueue_script('jQuery-js', 'http://code.jquery.com/jquery.js', array(), '1.0', true);
}
add_action('wp_enqueue_scripts', 'load_java_scripts');

So I think that I can add it as some other javascript or CSS local resources but I am not sure about this method because in this case the jquery.js is not a local resource but is an online resource (is the same thing?)

And I also have some doubts because searching online I have found some differents method to add JQuery to my theme, like this one: http://css-tricks.com/snippets/wordpress/include-jquery-in-wordpress-theme/

Can you give me some more precise information about how to correctly complete this task?

Tnx

Andrea

1
  • Why are you including an external copy of jquery? Is the included jquery library in WordPress not suitable for your uses?
    – t31os
    Commented Jan 21, 2014 at 19:08

1 Answer 1

6

When you register/enqueue your other scripts, simply pass jQuery as the third parameter and it will be loaded first and only once, from the local WordPress files:

wp_register_script( 'theme-js', get_template_directory_uri() . '/js/theme.js', array( 'jquery') );

If you only need the jQuery, simply use:

wp_enqueue_script('jquery');

You can find a list here about the javascript libraries that are included in your default WordPress site:

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

2
  • mmm are you meaning that WP automatically load JQuery in some way and that I have not to manually load it but that I can only pass it as dependency of the other JavaScript that require it? Commented Jan 21, 2014 at 11:41
  • 3
    I mean jQuery is in WordPress by default, located in the wp-includes folder, so you don't need to use an external source to include the javascript file. You can find all of the libraries included in WordPress in the codex, i updated my answer with a link. Also worth to mention, that jQuery is in no-conflict mode, so instead of $(document).ready(function(){... you should start with jQuery(document).ready(function($){...
    – passatgt
    Commented Jan 21, 2014 at 11:45

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.