KrisChase.com

How to load jQuery in WordPress theme

Posted in Web Development on 06.10.2015 by @chasebadkids

When building out a custom theme, one of the first things most developers will need in their set of tools and resources to write efficient code will be jQuery.  There are a number of ways you can add jQuery into your theme, but there is only one right way.

You might be tempted to download jQuery, upload it to your server and link to it directly in your header, but that isn’t the right way.

What you should do is utilize WordPress’s built in enqueue script functionality to load jQuery at the right time, the right way.

Put the following code in your themes functions.php file

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}

 

Menu