如何从 WordPress 的前端删除 jquery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1157531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:47:41  来源:igfitidea点击:

How can I remove jquery from the frontside of my WordPress?

jquerywordpress

提问by willdanceforfun

My wordpress site is a bit heavy to download. On the frontend, its including jquery unnecessarily. In my firebug it looks like:

我的 wordpress 网站下载量有点大。在前端,它不必要地包含 jquery。在我的萤火虫中,它看起来像:

jquery.js?ver=1.3.2

and

jquery.form.js?ver=2.02m

I don't need these to be included for me.

我不需要为我包含这些。

I'm happy for them to remain in the wp-admin, but I'd like them not to load on the frontend.

我很高兴他们留在 wp-admin 中,但我希望他们不要在前端加载。

I have found the file I think which is loading them in wp-includes/script-loader.phpbut I'm not sure what to uncomment out or what to do to remove it completely for the front.

我找到了我认为正在加载它们的文件,wp-includes/script-loader.php但我不确定要取消注释什么或如何将其完全删除。

Is there a way to do this, removing jquery without ruining the back end?

有没有办法做到这一点,在不破坏后端的情况下删除 jquery?

回答by deweydb

All the other solutions are now out of date as of wordpress 3.6

从 wordpress 3.6 开始,所有其他解决方案现在都已过时

add_filter( 'wp_default_scripts', 'change_default_jquery' );

function change_default_jquery( &$scripts){
    if(!is_admin()){
        $scripts->remove( 'jquery');
        $scripts->add( 'jquery', false, array( 'jquery-core' ), '1.10.2' );
    }
}

回答by NerdStarGamer

JQuery may be being added by your theme. If your theme is adding it properly, it should be using the wp_enqueue_script()function. To remove JQuery, simply use the wp_deregister_script()function.

您的主题可能正在添加 JQuery。如果您的主题正确添加它,它应该使用该wp_enqueue_script()功能。要删除 JQuery,只需使用该wp_deregister_script()函数。

wp_deregister_script('jquery');

Removing JQuery for your whole site might cause some unintended consequences for your admin section. To avoid removing JQuery on your admin pages, use this code instead:

删除整个站点的 JQuery 可能会给您的管理部分带来一些意想不到的后果。为避免在您的管理页面上删除 JQuery,请改用以下代码:

if ( !is_admin() ) wp_deregister_script('jquery');

Now only pages that are not admin pages will run the wp_deregister_script()function.

现在只有不是管理页面的页面才会运行该wp_deregister_script()功能。

Add this code to the functions.php file in your theme directory.

将此代码添加到主题目录中的functions.php 文件中。

回答by Pieter Goosen

The correct method to completely remove a style or script is to dequeue it andderegister it. You should also note that front end scripts are handled through the wp_enqueue_scriptshook while back end scripts are handled through the admin_enqueue_scriptshook.

完全删除样式或脚本的正确方法是将其出列注销。您还应该注意,前端脚本是通过wp_enqueue_scripts钩子处理的,而后端脚本是通过admin_enqueue_scripts钩子处理的。

So with that in mind, you can do the following

因此,考虑到这一点,您可以执行以下操作

add_filter( 'wp_enqueue_scripts', 'change_default_jquery', PHP_INT_MAX );

function change_default_jquery( ){
    wp_dequeue_script( 'jquery');
    wp_deregister_script( 'jquery');   
}

EDIT 1

编辑 1

This has been fully tested on Wordpress version 4.0 and working as expected.

这已经在 Wordpress 4.0 版上进行了全面测试,并按预期工作。

EDIT 2

编辑 2

As proof of concept, paste the following code in your functions.php. This will print a success or failure message in the head of your site, back end and front end

作为概念证明,将以下代码粘贴到您的functions.php 中。这将在您的站点、后端和前端的头部打印成功或失败的消息

add_action( 'wp_head', 'check_jquery' );
add_action( 'admin_head', 'check_jquery' );
function check_jquery() {

    global $wp_scripts;

    foreach ( $wp_scripts->registered as $wp_script ) {
        $handles[] = $wp_script->handle; 
    }

    if( in_array( 'jquery', $handles ) ) {
        echo 'jquery has been loaded';
    }else{
        echo 'jquery has been removed';
    }
}

回答by Adam Tal

Wordpress adds this jQuery call via a template tag named <?php wp_head(); ?>, which appears in most themes, and is necessary for some plugins to work.

Wordpress 通过名为 的模板标签添加了这个 jQuery 调用,该标签<?php wp_head(); ?>出现在大多数主题中,并且是某些插件工作所必需的。

It could be annoying, not only because of loading, but because it might kill previously loaded jQuery, and might even get in the way of some plugins who try to load jQuery as well.

这可能很烦人,不仅因为加载,还因为它可能会杀死以前加载的 jQuery,甚至可能会妨碍一些尝试加载 jQuery 的插件。

The quick fix is openning up the file header.php in your theme's directory, and adding:

快速修复是打开主题目录中的文件 header.php,并添加:

<?php wp_deregister_script('jquery'); ?>

right before

就在之前

<?php wp_head(); ?>

Or just combine them both into:

或者只是将它们组合成:

<?php wp_deregister_script('jquery'); wp_head(); ?>

A more technical explanation could be found here

可以在此处找到更技术性的解释

回答by user25

function my_init() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', false);
    }
}
add_action('init', 'my_init');

It's correct - removes jquery library js. Code from other answers removes all js (even js that your installed plugins adds)

这是正确的 - 删除了 jquery 库 js。其他答案中的代码会删除所有 js(甚至是您安装的插件添加的 js)

Tested on 4.3.1

在 4.3.1 上测试

回答by Ahmet Kak?c?

Look into your theme files.

查看您的主题文件。

wp-content/themes/header.php

wp-content/themes/header.php

may include the .js files.

可能包含 .js 文件。

回答by Jon Limjap

jQuery.js is just 15KB if you're using the minified version, and these would be totally absent if you were using a theme that doesn't require it.

如果您使用的是缩小版,jQuery.js 只有 15KB,如果您使用的是不需要它的主题,那么这些将完全不存在。

You should probably look for a lightweight theme without jQuery instead of hacking it and then seeing the theme break in several places because they're looking for those js files.

您可能应该寻找一个没有 jQuery 的轻量级主题,而不是破解它,然后在几个地方看到主题中断,因为他们正在寻找那些 js 文件。

回答by markratledge

Look in the source of your rendered page; Wordpress often includes jQuery by default when <?php wp_head(); ?>is called in header.php, so you may stil see jQuery included in your site.

查看渲染页面的来源;Wordpress 通常<?php wp_head(); ?>在 header.php 中调用时默认包含 jQuery ,因此您可能仍然会看到 jQuery 包含在您的站点中。

If you remove <?php wp_head(); ?>in header.php, you might loose other plugin functionality, as many plugins "hook" into Wordpress at that point.

如果您<?php wp_head(); ?>在 header.php 中删除 ,您可能会失去其他插件功能,因为此时许多插件“挂钩”到 Wordpress 中。

But including jQuery isn't that big of a deal. It's small and Wordpress depends on it for some things.

但是包含 jQuery 并不是什么大不了的事。它很小,Wordpress 在某些方面依赖于它。

回答by Dexter

WordPress 5 and above (Tested)

WordPress 5 及以上(已测试

Remove the default jquery and add your jquery from folder or from CDN. Use only one, 'local' or 'cdn'

删除默认的 jquery 并从文件夹或 CDN 添加您的 jquery。只使用一个,'local' 或 'cdn'

// Remove the WordPress default jquery
wp_deregister_script( 'jquery' );

// using a local file
wp_enqueue_script(
  'jquery', get_template_directory_uri() . '/lib/jquery-3.3.1.min.js','', '3.3.1', true
);

// using CDN
wp_enqueue_script(
    'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', '', '3.3.1', true
);

// $handle: 'jquery'
// $src: 
    // local: get_template_directory_uri() . '/lib/jquery-3.3.1.min.js'
    // cdn: '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'
// $deps: '' (leave it empty)
// $ver: '3.3.1'
// $in_footer: true (boolean)

Syntax

句法

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );

回答by Heath

I was able to shave 2.2 seconds off my "Events" page load speed by de-registering jQuery. jQuery is a nice to have but in my opinion page speed is so much more important.

通过取消注册 jQuery,我能够将“事件”页面加载速度缩短 2.2 秒。jQuery 是一个不错的选择,但在我看来页面速度更为重要。

You'll never have a user hang around for more than 5 seconds so if jQuery is causing you performance problems, then I say get rid.

您永远不会让用户停留超过 5 秒,因此如果 jQuery 导致您出现性能问题,那么我会说摆脱。