macmiller
macmiller

Reputation: 324

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?

Upvotes: 3

Views: 24440

Answers (7)

imnhasan
imnhasan

Reputation: 115

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

Upvotes: 0

Nandkishor
Nandkishor

Reputation: 11

if( !is_admin()){
  wp_deregister_script('jquery');
  wp_register_script('jquery',       ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"), false, '1.3.1');
  wp_enqueue_script('jquery'); 
}

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

Upvotes: 0

Sparky
Sparky

Reputation: 98748

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.)

Upvotes: -1

enam
enam

Reputation: 1177

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', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3'); 
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'include_jQuery');

Upvotes: 0

Ionuț Staicu
Ionuț Staicu

Reputation: 22204

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

Upvotes: 0

Paul Sham
Paul Sham

Reputation: 3205

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: http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/

Upvotes: 6

Ruslan Abuzant
Ruslan Abuzant

Reputation: 631

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.

Upvotes: 0

Related Questions