Post Pic

Adding jQuery to a custom WordPress theme

I have been doing a little bit of WordPress work recently and as a result I have been working on custom themes for WordPress. In one particular theme I required that jQuery be loaded into the template and stumbled across the following wp_enqueue_script function built into wordpress. The function essentially adds a safe method of including scripts in WordPress generated pages.


I first needed to locate the following line in my head.php file:

<?php wp_head(); ?>

Then before the call to this function I can add my call to the wp_enqueue_script function so that we end up with the following:

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

What this does is includes the WordPress instance of jQuery however it is arguable about the efficiency of this method over loading it direct from Google servers since if everyone used this method users would not need to take the hit downloading the file as there would be a greater chance of the file aready being in their cache. It is beyond the scope of this post to go into more detail on this however if you are interested in reading more you may want to swing by Encosia and read 3 reasons why you should let Google host jQuery for you. If you want to opt for this method you can simply amend the above example as follows:

 wp_register_script('jquery', '', false, '1.3.2');
<?php wp_head(); ?>

Umbraco Certified Developer
SagePay Approved Partner
Creative Market