Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 627
macmiller Avatar asked Aug 21 '26 15:08

macmiller


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

like image 126
Paul Sham Avatar answered Aug 24 '26 06:08

Paul Sham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!