Enqueuing jQuery for WordPress Theme

, , ,

Here is the function to add jQuery to the front end of the site.  You just need to enqueue the script labeled jquery.

if (!is_admin()) add_action("wp_enqueue_scripts", "default_jquery_enqueue", 11);
function default_jquery_enqueue() {
   wp_enqueue_script('jquery');
}

If for some reason you would like to enqueue a different version you can do so by de-registering jquery and then re-registering it with the version you would like.

if (!is_admin()) add_action("wp_enqueue_scripts", "different_ver_jquery_enqueue", 11);
function different_ver_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');
}

This only changes the front end so the backend is still using the jQuery version it needs.

Skills

Posted on

December 9, 2016

Submit a Comment

Your email address will not be published. Required fields are marked *