Wednesday, December 8, 2010

Wordpress and jQuery: Where's the Dollar?

The first thing I did in Wordpress was add jQuery, using the normal tag:

wp_enqueue_script( 'jquery' );

right above wp_head();

But then $ was not recognized? Why? Because Wordpress loads in noConflict mode. To put jQuery back for a particular page, use:

// set the _$ which jQuery uses for backup
window._$ = $;
// set the $ to jQuery
window.$ = jQuery;

Another option is to put this above the wp_enqueue_script, which will load jQuery in normal conflicty mode.

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', '', false, '' );

See the meandering Stackoverflow thread here.

No comments: