Using Google CDN to load jQuery, with a local WordPress fallback?
-
Hi,
I’m considering if it’s worth while, dequeuing WordPress’ jQuery for Google CDN jQuery, being a faster load?
Possible checking on the current WordPress version:
$jquery_version = $GLOBALS['wp_scripts']->registered['jquery']->ver; wp_enqueue_script('jquery','https://ajax.googleapis.com/ajax/libs/jquery/' . $jquery_version . '/jquery.min.js'
I would probably need to check to make sure Google’s library is available?
Under the theme function would it be best to check/replace?
Also, only do it if not admin?
Something like…
//jQuery Insert From Google if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11); function my_jquery_enqueue() { $jquery_version = $GLOBALS['wp_scripts']->registered['jquery']->ver; wp_deregister_script('jquery'); wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery" . $jquery_version . "/jquery.min.js", false, null); wp_enqueue_script('jquery'); }
Is it worth checking the URL is available first, as that would be a lot of pinging? Or is it better to just manually update the version each WordPress upgrade?
Would I also need to do ‘jquery-migrate.min.js’ too?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Using Google CDN to load jQuery, with a local WordPress fallback?’ is closed to new replies.