How to include jQuery in WordPress

Cat: jQuery Examples - Cut and Paste Scripts, WordPress Scripts & Plugins



If you want, you can just download jQuery, put it on your server and link to it from your header.php file in the section. But that can cause you grief. For one thing, some plugins use the jQuery library, and they are going to load it as well. This can cause problems. How was your plugin to know you already had it loaded?

Another thing is that WordPress already includes a copy of jQuery.
Here is how you can load up jQuery in your theme the smart (and intended) way.
Put the following code in your header.php file in the section:

<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>
</head>

Your theme probably already has the wp_head function, so make sure you call the wp_enqueue_script function BEFORE that. Now you are all set to call your own jQuery JavaScript file, AFTER the wp_head function.

<script type="text/javascript"
src="/js/yourScript.js">

Drop this in a page or post to see if you've added the files correctly:

<script>
$(document).ready(function(){
alert('Hi world');
});
</script>

Comments are closed.

Featured & Popular Articles