How can I remove jquery from the frontside of my W

2020-02-24 07:18发布

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

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.

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

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

10条回答
做个烂人
2楼-- · 2020-02-24 07:57

All the other solutions are now out of date as of 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' );
    }
}
查看更多
一纸荒年 Trace。
3楼-- · 2020-02-24 08:01

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.

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.

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

<?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

查看更多
唯我独甜
4楼-- · 2020-02-24 08:02

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.

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楼-- · 2020-02-24 08:12

The correct method to completely remove a style or script is to dequeue it and deregister it. You should also note that front end scripts are handled through the wp_enqueue_scripts hook while back end scripts are handled through the admin_enqueue_scripts hook.

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

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

EDIT 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

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';
    }
}
查看更多
登录 后发表回答