1

I need to add jQuery library to WordPress page. The page is rendered by a plugin shortcode.

I've used the below methods but I don't see it working for my jQuery script.

I tried to add these below script in main plugin file where the plugin name and version go.

DESN'T WORK:

function my_init() {
    if (!is_admin()) {
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'my_init');

DESN'T WORK

function my_init() {
    if (!is_admin()) {
        // comment out the next two lines to load the local copy of jQuery
        wp_deregister_script('jquery'); 
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, '1.4.2'); 
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'my_init');

I actually need to add below library to my WordPress front end page?

http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

but if i use it directly like below then it works.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Second, How can see it loaded in my browser loaded along with other code?

  • You can see if your browser has loaded the script in the network panel of your development tools. Does your theme include a call to wp_head() – veganista Apr 29 '15 at 13:21
  • It is highly recommended to use the jQuery version that comes with WordPress core. – Nicolai Apr 29 '15 at 13:55
  • Hook your action onto wp_enqueue_scripts, not init. Your first example should work fine then, assuming, as @veganista pointed out, that your theme calls wp_head() – Andrew Bartel Apr 29 '15 at 20:03
0

I think wordpress only loads jQuery when you specify it is a dependency for another script that is loading on that page:

wp_enqueue_script( 'script-name', '/path/script.js', array('jQuery');

If there is no other script then try loading it like a normal script with full path to the file:

wp_enqueue_script( 'myJquery', '//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');

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.