WordPress 禁用“regenerator-runtime-js”和“wp-polyfill-js”

问题描述 投票:0回答:3

这两个脚本被添加到我的 WordPress 页面的每个页脚上。是否可以通过functions.php删除它们?

wordpress
3个回答
5
投票

functions.php 中的以下代码应删除 wp-polyfill 和 regenerator-runtime:

function deregister_polyfill(){

  wp_deregister_script( 'wp-polyfill' );
  wp_deregister_script( 'regenerator-runtime' );

}
add_action( 'wp_enqueue_scripts', 'deregister_polyfill');

另请参阅:wp_deregister_script

编辑:进一步调查问题后,我发现这些样式包含在Contact Form 7插件中。我对这个问题的解决方案是编写一个“必须使用插件”,以便仅当您处于联系表单中时才包含“联系表单 7”插件。 为此目的,我创建了以下文件:

wp-content/mu-plugins/tk-mu-plugins.php

填写如下:

<?php /* Plugin Name: TK-MU-Plugin Description: Author: Thomas Krakow Version: 1.0 Author URI: https://thomas-krakow.de */ $fRequestURI = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); if( false === strpos( $fRequestURI, '/wp-admin/' ) ){ add_filter( 'option_active_plugins', 'tk_activate_plugins' ); } function tk_activate_plugins( $plugins ){ global $fRequestURI; $is_contact_page = strpos( $fRequestURI, '/contact/' ); $k = array_search( "contact-form-7/wp-contact-form-7.php", $plugins ); if( false !== $k && false === $is_contact_page ){ unset( $plugins[$k] ); } return $plugins; }

我在 GitHub 上的代码要点:
tk-mu-plugin.php

我的博客上的德语解释:

CSS und Javascript nur bei Bedarf auf WordPress-Seiten einfügen


1
投票

wp_deregister_script('wp-polyfill'); wp_deregister_script('regenerator-runtime');



0
投票

通常包含 Polyfill 以确保与旧版浏览器的兼容性。但是,如果您的网站主要服务于现代浏览器,它们可能会增加不必要的加载时间。本指南将帮助您使用主题的functions.php文件中的自定义代码从WordPress中删除polyfill。

第1步:打开主题的functions.php文件

导航到您的主题目录:wp-content/themes/your-theme/。
  1. 使用文本编辑器或WordPress主题编辑器打开functions.php文件。
  2. 第 2 步:添加代码以删除 Polyfill 删除 WordPress Polyfill 此代码片段将使默认的 WordPress polyfill 脚本出队并取消注册。

function remove_polyfill() { wp_dequeue_script('wp-polyfill'); wp_deregister_script('wp-polyfill'); } add_action('wp_enqueue_scripts', 'remove_polyfill', 100);

说明:

• wp_dequeue_script('wp-polyfill');:该函数从队列中删除polyfill 脚本。 • wp_deregister_script('wp-polyfill');:该函数取消注册脚本,因此不会将其添加回来。 删除 Intersection Observer Polyfill 如果您的网站包含 Intersection Observer polyfill,您可以使用以下代码将其删除。

function remove_intersection_observer_polyfill() { wp_dequeue_script('intersection-observer-pollyfill'); wp_deregister_script('intersection-observer-pollyfill'); } add_action('wp_enqueue_scripts', 'remove_intersection_observer_polyfill', 100);

说明:

wp_dequeue_script('intersection-observer-pollyfill');:从队列中删除 Intersection Observer polyfill 脚本。 wp_deregister_script('intersection-observer-pollyfill');: 取消注册脚本以防止其被重新添加。

删除不需要的资产(包括 DNS 预取和另一个 polyfill 句柄)

要删除其他不需要的资产,例如 DNS 预取和 Intersection Observer polyfill 的另一个实例,请使用此代码段。

function remove_unwanted_assets() { // Remove DNS prefetch link remove_action('wp_head', 'wp_resource_hints', 2); // Dequeue and deregister Intersection Observer polyfill script wp_dequeue_script('intersection-observer-polyfill-js'); wp_deregister_script('intersection-observer-polyfill-js'); } add_action('wp_enqueue_scripts', 'remove_unwanted_assets', 100);

说明:

remove_action('wp_head', 'wp_resource_hints', 2);: Removes DNS prefetch links from the head. wp_dequeue_script('intersection-observer-polyfill-js');: Dequeues another instance of the Intersection Observer polyfill. wp_deregister_script('intersection-observer-polyfill-js');: Deregisters the script.

第3步:保存并测试

Save the changes to your functions.php file. Clear your browser cache, any WordPress caching plugins, and CDN caches to ensure changes take effect. Test your website to ensure that functionality remains intact and that there are no JavaScript errors. Open the browser console (F12 or right-click and select "Inspect" > "Console") to check for errors.

第 4 步:验证性能

使用 Google PageSpeed Insights 等工具重新运行性能测试,以确认 Polyfill 不再被加载,并观察页面加载时间的任何改进。

我已经尝试了很多方法,但到目前为止这对我的网站有效,希望听取其他人的意见,并希望其他人分享其他想法或更简单的解决方案

© www.soinside.com 2019 - 2024. All rights reserved.