用Google库替换默认的WordPress jQuery脚本

巫新知
2023-12-01

There are default scripts that comes with each WordPress installation. These scripts are at disposal of theme and plugin authors to use in their work. From our experience, jQuery is one of the most used scripts in plugins and themes. In this article, we will show you how to replace the default WordPress jQuery script with Google Library script.

每种WordPress安装随附默认脚本 。 这些脚本可供主题和插件作者在其工作中使用。 根据我们的经验,jQuery是插件和主题中最常用的脚本之一。 在本文中,我们将向您展示如何用Google库脚本替换默认的WordPress jQuery脚本。

You are probably wondering why should you replace the default WordPress jQuery script with the one from Google Library? jQuery is being used on a lot of sites including the big ones. Google Library has become the standards for including scripts in web design. It could be because Google Libraries are using Google’s CDN which is extremely powerful. Because it has now become a standard, a lot of other sites are utilizing it as well. Chances are that your website user has also visited one of those other sites that are using Google’s Library for scripts. So the script is already cached in your user’s browser. This makes loading of the jQuery script very fast on user’s end. In today’s world when we are fighting for each microseconds and optimizing our site, who wouldn’t love to cut out a microsecond from the loading time?

您可能想知道为什么要用Google图书馆的脚本替换默认的WordPress jQuery脚本? jQuery已在许多网站上使用,包括大型网站。 Google图书馆已成为在网页设计中包含脚本的标准。 可能是因为Google图书馆正在使用功能极为强大的Google CDN。 因为它现在已经成为标准,所以许多其他站点也正在使用它。 您的网站用户很可能还访问了其他使用Google的库编写脚本的网站之一。 因此,脚本已经缓存在用户的浏览器中。 这使得在用户端非常快速地加载jQuery脚本。 在当今世界,我们为每个微秒而奋斗并优化我们的网站时,谁会不愿意为加载时间减少一微秒呢?

Now you are probably wondering, how do I do this? Well its pretty simple. Open your theme’s functions.php file and paste the following snippet:

现在您可能想知道,我该怎么做? 好吧,它很简单。 打开主题的functions.php文件,并粘贴以下代码段:

//Making jQuery Google API
function modify_jquery() {
	if (!is_admin()) {
		// comment out the next two lines to load the local copy of jQuery
		wp_deregister_script('jquery');
		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js', false, '1.8.1');
		wp_enqueue_script('jquery');
	}
}
add_action('init', 'modify_jquery');

You are done with this. While we are talking about optimizing WordPress load time, you should also look at How WordPress plugins affect your site’s load time. Another good resource would be how to add scripts in WordPress the right way.

您已经完成了。 在讨论优化WordPress的加载时间时,您还应该查看WordPress插件如何影响网站的加载时间 。 另一个好的资源是如何以正确的方式在WordPress中添加脚本

Note: You should not do this on theme’s or plugins that you will not be controlling forever (i.e client sites, free/commercial theme or plugin that you want to distribute to the masses). On your own site, this trick brings performance benefits. But if you don’t keep your jQuery up to date, then you run the risk of having plugin compatibility issues. You can always use a plugin: Use Google Libraries. This plugin allows your site to use common javascript libraries from Google’s AJAX Libraries CDN, rather than from WordPress’s own copies. It also keeps the libraries up to date as long as you keep that plugin up to date.

注意:您不应在永远无法控制的主题或插件上执行此操作(例如,要分发给大众的客户端站点,免费/商业主题或插件)。 在您自己的站点上,此技巧可带来性能优势。 但是,如果您不更新jQuery,则会冒插件兼容性问题的风险。 您始终可以使用插件: 使用Google库 。 这个插件可让您的网站使用Google AJAX库CDN中的通用javascript库,而不是WordPress自己的副本。 只要您使该插件保持最新,它还可以使库保持最新。

翻译自: https://www.wpbeginner.com/wp-themes/replace-default-wordpress-jquery-script-with-google-library/

 类似资料: