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

前端 未结 10 887
逝去的感伤
逝去的感伤 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 02:53

    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 );
    

提交回复
热议问题