Quantcast
Channel: Extreme Web Designs » wordpress-snippets
Viewing all articles
Browse latest Browse all 10

Call / Include / Load Latest jQuery Version in WordPress Theme / Plugin

$
0
0

Robert

If you are wondering how to Call / Include / Load Latest jQuery Version in WordPress Theme / Plugin, then look no further! In this article, I am going to share a very simple way that use can use to always load the latest jQuery version either in your Theme or Plugin or both. Read on to find out more.

How to Call / Include / Load Latest jQuery Version in WordPress Theme / Plugin

One of the strongest reason to include latest jQuery version is to take advantage of new features that jQuery offers without having to manually change the link pointer to latest jQuery version. That said, following are the ways to load latest jQuery version in WordPress:

Load Latest jQuery Version in WordPress Theme:

Paste the following code in your theme’s function.php file:

function ewd_load_js()
{
if ( !is_admin() )
{
wp_deregister_script( 'jquery' );

wp_register_script('jquery', 'http://code.jquery.com/jquery-latest.min.js');

wp_enqueue_script('jquery');
}
}

add_action('wp_enqueue_scripts', 'ewd_load_js');

Load Latest jQuery Version in WordPress Plugin

Or if you need the latest jQuery version to be loaded in your custom plugin, then simply paste the following code in your plugin’s main file:

function ewd_load_js_plugin()
{
wp_deregister_script( 'jquery' );

wp_register_script('jquery', 'http://code.jquery.com/jquery-latest.min.js');

wp_enqueue_script('jquery');

}

add_action('wp_enqueue_scripts', 'ewd_load_js_plugin');

Load JavaScript(s) in Footer

function ewd_load_js_plugin()
{
wp_deregister_script( 'jquery' );

wp_register_script('jquery', 'http://code.jquery.com/jquery-latest.min.js', 'false', 'false', true);

wp_enqueue_script('jquery');

}

add_action('wp_enqueue_scripts', 'ewd_load_js_plugin');

So what is different in the above code? Well, if you pay close attention, we now have some new arguments appended in the wp_register_scripts function. Especially the argument “true”.  This corresponds to the $in_footer variable and it is this value that is responsible loading the scripts in footer. Here would be the actual usage of wp_register_script:

<?php wp_register_script( $handle, $src, $deps, $ver, $in_footer ); ?>

$handle – Name of the script

$src – URL to script

$deps – Array of handles of all the registered scripts that this script depends on, that is the scripts that must be loaded before this script.

$ver - String specifying the script version number, if it has one, which is concatenated to the end of the path as a query string.

$in_footer - Normally scripts are placed in the <head> section. If this parameter is true the script is placed at the bottom of the <body>. This requires the theme to have the wp_footer() hook in the appropriate place.

Your turn!

Do you know of any other ways to Call / Include / Load Latest jQuery Version in WordPress Theme / Plugin? Feel free to suggest by commenting below.

Call / Include / Load Latest jQuery Version in WordPress Theme / Plugin


Viewing all articles
Browse latest Browse all 10

Trending Articles