Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a jQuery script that works fine within a WordPress post as long as I include a reference to the jQuery libarary within the post.

<script src="http://code.jquery.com/jquery-latest.js"></script>

From what I have read this is not the proper way to reference jQuery as it is already included within WordPress (using v3.4.2).

If I remove the inline reference the script fails and the console says that "$" is undefined. I also tried replacing "$" with "jQuery" according to articles that talk about NoConflict mode in jQuery.

My jQuery script can be viewed here in jsFiddle.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

In order to use $ inside your script (You only need to change the wrapper of your script), use the following wrapper.

jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
});

Make sure you enqueue jQuery in one of your plugins or theme using wp_enqueue_script('jquery');

Read more on Codex

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

The following article contains useful tips on using jQuery with WordPress.

http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/

share|improve this answer
    
+1 Thanks! That worked great. I changed the wrapper of the script and added the wp_enqueue_script('jquery'); to my functions.php file in my theme using the method explained in WP codex link you posted. –  webworm Dec 10 '12 at 2:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.