4

I am trying to learn WP theme development. And I just learned that WP already has a jQuery version included. Until now I have been using the jQuery from Google's CDN.

So I just now deleted the CDN link from my code. And of course all my jQuery code stopped working.

So I guess I am missing something. What do I need to do if I want to use WordPress's local jQuery?

When I search SE QA and Google all I can find is how to un-register the local jQuery version and then register CDN. But that is not what I want to do. I want to use the local jQuery.

So what am I missing?

  • search WPSE before you ask a question. It's great possibility that it has been answered already. wordpress.stackexchange.com/a/111569 – Robert hue Sep 2 '15 at 15:27
  • Well I did search first. And I also said I already been searching in my Question. And all I could find was how to deregister and that was not what I was trying to do :) – Alex Sep 2 '15 at 15:36
4

WP's jQuery is not mapped to $ like you may expect, it is loaded in noConflict mode so you'll need to use jQuery() instead of $(), unless you map it yourself.

When you include your js file you'll want to make sure to set jQuery as a dependency as well:

wp_enqueue_script( 'your-script-handle', get_stylesheet_directory_uri() . '/js/your-script.js', array( 'jquery' ), '1.0.0' );
| improve this answer | |
1

In order to enqueue jQuery from WP-installation you can:

function custom_enqueue_scripts() {
    // Jquery enqueue
    wp_enqueue_script('jquery');
}

add_action('wp_enqueue_scripts', 'custom_enqueue_scripts');

Additionally, using this trick you can enqueue jQuery into footer (wp_footer)

function custom_enqueue_scripts() {
    // Jquery - to the footer of template
    wp_deregister_script('jquery');
    wp_register_script('jquery', includes_url('/js/jquery/jquery.js'), false, NULL, true);
    wp_enqueue_script('jquery');
}

add_action('wp_enqueue_scripts', 'custom_enqueue_scripts');

UPDATE

Expanding the answer.

Using wp_register_script you can register your custom scripts. After, they can be wp_enqueue_script by the $handler.

Generally it grant a benefit of code length reduction [Related Answer], but I'd like to emphasize your attention on WordPress pre-registered scripts (local in your meaning).

Visit this Codex Page [Handles and Their Script Paths Registered by WordPress]. There you will find a $handler's for jQuery UI and other usefull stuff, that you can enqueue as on example above.

| improve this answer | |

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.