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

前端 未结 10 833
逝去的感伤
逝去的感伤 2020-12-29 02:33

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


        
相关标签:
10条回答
  • 2020-12-29 03:01

    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.

    0 讨论(0)
  • 2020-12-29 03:03

    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.

    0 讨论(0)
  • 2020-12-29 03:04

    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.

    0 讨论(0)
  • 2020-12-29 03:08

    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' );
        }
    }
    
    0 讨论(0)
提交回复
热议问题