1

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.

2

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/

  • +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

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.