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:46

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.

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.

查看更多
Deceive 欺骗
3楼-- · 2020-02-24 07:46

WordPress 5 and above (Tested)

Remove the default jquery and add your jquery from folder or from CDN. Use only one, 'local' or '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 );
查看更多
Rolldiameter
4楼-- · 2020-02-24 07:49

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.

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

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

查看更多
劳资没心,怎么记你
5楼-- · 2020-02-24 07:50

Look into your theme files.

wp-content/themes/header.php

may include the .js files.

查看更多
走好不送
6楼-- · 2020-02-24 07:52

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.

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:

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

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

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

查看更多
混吃等死
7楼-- · 2020-02-24 07:54
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)

Tested on 4.3.1

查看更多
登录 后发表回答