WordPress: Load jQuery from a CDN

WordPress usually loads jQuery as a normal file. However, to improve the loading speed of the blog, it should be loaded from a CDN (for example from Google). This can improve the loading time because the CDN offers the possibility to efficiently distribute and deliver resources like jQuery.

The following code simply needs to be inserted into the functions.php of the theme, for example at the end before the final „?>“ tag:

function jquery_googleCDN() {
      wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js', array(), '3.6.0', true);
add_action('init', 'jquery_googleCDN');

This code removes the local version of jQuery loaded in WordPress and registers the version of jQuery on the Google CDN instead. You can modify the URL of the CDN to load jQuery from a different CDN if desired. Once you have added the code and saved the functions.php file, jQuery will be loaded from the specified CDN when your WordPress website is accessed.

Sharing is caring...

Posted by Phil