Skip to content

Making jQuery scripts work in WordPress

July 6, 2012

Most front-end developers working in WordPress are too lazy to code their own jQuery animations and functions when they want to do something they know has been done hundreds of times before.

So we grab jQuery code from a website and shove it into WordPress… and usually nothing happens.

Assuming you’ve already loaded in the jQuery library correctly this can be very frustrating. To save you lots of time fiddling about, this is probably the reason:

Where you have a function such as…

$(document).ready(function(){
$('body').addClass('something');
});
});

You need to change the ‘$’ to ‘jQuery’ like so…

jQuery(document).ready(function(){
jQuery('body').addClass('something');
});
});

WordPress doesn’t like the ‘$‘ sign being used to denote a jQuery action, but it’s quite happy if you spell it out. It took me a long time to figure this out first time around and has often caught me out since!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s