JQuery Not Working in Wordpress

I am using a test environment to try to get jquery working in wordpress, the installation version is 3.2.1 and the theme is 2010 (although I have tried this in a few different themes, same result). There are no plug-ins installed.

Basically I am putting this in the header file to see if I can get jQuery to work.

<script type="text/javascript">
jQuery(document).ready(function(){
alert('test');
    });
</script>

have also tried this

jQuery(document).ready(function($){

and this

$j=jQuery.noConflict(); 
// Use jQuery via $j(...)
$j(document).ready(function(){
  alert('test');
});

I can not seem to get it to display the alert when the page loads. When I check firebug the script has loaded.

jQuery works fine when not used in conjunction with WP and all scripts tested outside of WP perform as expected.

Should it work in WP 3.2.1? What can I try?

1

7 Answers

Have you added the actual jQuery script file?

There are a number of ways to do this, but I usually use the following method by including this code in the functions.php file.

<?php
    function add_jquery() {
       wp_enqueue_script( 'jquery' );
    }    

    add_action('init', 'add_jquery');
?>

Here is also a good resource for more jQuery + Wordpress usage:

1

Make sure WP itself (or another installed plugin) does not have an already embedded jQuery version that conflicts with yours.. View the source of your page and make sure only one jQuery version is loaded. Should go w/o problems.

By using wp_enqueue_script you are sure the jQuery library is loaded ONCE. You can see here how to load a JS file.

Beside the way mentioned by Paul Sham which is absolutely correct, following way is also helpful which is also describe at THIS link.

function include_jQuery() {
    if (!is_admin()) {
        // comment out the next two lines to load the local copy of jQuery
        wp_deregister_script('jquery'); 
        wp_register_script('jquery', ' false, '1.8.3'); 
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'include_jQuery');
if( !is_admin()){
  wp_deregister_script('jquery');
  wp_register_script('jquery',       (""), false, '1.3.1');
  wp_enqueue_script('jquery'); 
}

put this code in your current wordpress theame's function.php file in between

I find the other one, write this at the beginning of your script. $ = jQuery.noConflict();

EDIT:

The answer below was not intended as step-by-step instructions about how to add jQuery to Wordpress. I'm simply asking him to check and see if it's already there, then I'm telling him to "not include it again" IF it's already there.

As mentioned in comments, wp_enqueue_script is the absolute best way to add jQuery to Wordpress, whenever you need to add jQuery.


Of course jQuery works within Wordpress but I am not sure if it's already included within the Twenty-Ten theme by default. Check to see if the script includes tags are there. You need something like this in the header someplace before you can call any JavaScript that uses jQuery...

<script type='text/javascript' src='/jquery.js?ver=1.6.1'></script>

(Make sure you're not including it more than once or multiple versions.)

8

Your Answer

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

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest