Created
September 2, 2012 10:58
-
-
Save ArupSen/3596847 to your computer and use it in GitHub Desktop.
Use jquery on a wordpress site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? php | |
function my_init() { | |
if (!is_admin()) { | |
// comment out the next two lines to load the local copy of jQuery | |
wp_deregister_script('jquery'); | |
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js', false, '1.5.2'); | |
wp_enqueue_script('jquery'); | |
} | |
} | |
add_action('init', 'my_init'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JQuery comes bundled with wordpress from 2.4 (I think) onwards. In order to use a jquery script within a template three things are required. Firstly the add the above snippet to the functions.php file. The function my_init will then call the jquery library either from wordpress itself or from the google server. The google way is a better alternative as lots of sites use that link and it will probably already be cached in the users browser, which of course will make things faster. The wp_head(); needs to be in the header.php of course.
Secondly I've found that placing your jquery script inside header.php doesn't seem to work. So I simply embedded it inside script tags within a custom page template and that does the job.
Thirdly, you can't use the $ symbol to represent the jquery object because wordpress is in noConflict mode so you have to use jquery or rather jQuery instead. For a short script this is fine but for longer scripts it may be worth coming out of noConflict mode to avoid the code clutter. Find / Replace doesn't take long though.