Skip to main content

Wordpress - remove unneeded JS and CSS from a page

https://stackoverflow.com/questions/35980246/remove-js-and-css-from-a-template-in-wordpress

Try to use this in functios.php first:
function remove_head_scripts() {   global $wp_styles;   var_dump($wp_styles);   global $wp_scripts;   var_dump($wp_scripts);}
add_action( 'wp_enqueue_scripts', 'remove_head_scripts', 101 );
Then you can see all of the enqueued styles & scripts with their respective slugs printed.
After you know exact slugs of your script & style, replace snippet above with new one:
function remove_head_scripts() { wp_dequeue_style   wp_dequeue_style( 'exact_style_slug' );   wp_dequeue_script( 'exact_script_slug' );}
add_action( 'wp_enqueue_scripts', 'remove_head_scripts', 101 );
____________________________________________________________
This is not my code:
// Deregister Contact Form 7 styles in your functions.php for any page that isn't your contact page. (assuming your page is named contact-us)
add_action( 'wp_print_styles', 'aa_deregister_styles', 100 );function aa_deregister_styles() {   if ( ! is_page( 'contact-us' ) ) {     wp_deregister_style( 'contact-form-7' );   }}
// Deregister Contact Form 7 JavaScript files on all pages without a form
add_action( 'wp_print_scripts', 'aa_deregister_javascript', 100 );function aa_deregister_javascript() {   if ( ! is_page( 'contact-us' ) ) {     wp_deregister_script( 'contact-form-7' );   }}
____________________________________________________________

Remove scripts that won’t work with wp_dequeue_script

Sometimes scripts are added differently. The most common way would be to simply output the script or style tag directly in the header or footer.

The entire plugin

That’s about it. You can copy / paste the entire plugin from here and make modifications to it to better suit your WordPress site.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
 * Plugin Name: WP Remove Assets
 * Plugin URI: https://www.cozmsolabs.com
 * Version: 0.1
 * Description: Filter particular scripts and style to load in posts or pages that don't need it.
 * Author: Cristian Antohe
 * Author URI: https://www.cozmoslabs.com/
*/
 
 
// remove script handles we don't need, each with their own conditions
 
add_action('wp_print_scripts', 'wra_filter_scripts', 100000);
add_action('wp_print_footer_scripts',  'wra_filter_scripts', 100000);
 
function wra_filter_scripts(){
    #wp_deregister_script($handle);
    #wp_dequeue_script($handle);

    wp_deregister_script('bbpress-editor');
    wp_dequeue_script('bbpress-editor');
 
    // Device Pixels support
    // This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. We only have gravatars so we should be ok without it.
    wp_deregister_script('devicepx');
    wp_dequeue_script('devicepx');
 
    if( !is_singular( 'docs' ) ){
        // the table of contents plugin is being used on documentation pages only
        wp_deregister_script('toc-front');
        wp_dequeue_script('toc-front');
    }
 
    if( !is_singular( array('docs', 'post' ) ) ){
        wp_deregister_script('codebox');
        wp_dequeue_script('codebox');
    }
 
 
}
 
// Jetpack
add_action('jetpack_implode_frontend_css', 'wra_remove_jetpack');
function wra_remove_jetpack(){
    return false;
}
 
 
 
// remove styles we don't need
add_action('wp_print_styles', 'wra_filter_styles', 100000);
add_action('wp_print_footer_scripts', 'wra_filter_styles', 100000);
function wra_filter_styles(){
 
    #wp_deregister_style($handle);
    #wp_dequeue_style($handle);

    //no more bbpress styles.
    wp_deregister_style('bbp-default');
    wp_dequeue_style('bbp-default');
 
    // download monitor is not used in the front-end.
    wp_deregister_style('wp_dlmp_styles');
    wp_dequeue_style('wp_dlmp_styles');
 
    if( !is_singular( 'docs' ) ){
        // the table of contents plugin is being used on documentation pages only
        wp_deregister_style('toc-screen');
        wp_dequeue_style('toc-screen');
    }
 
    if ( !( is_page( 'account' ) || is_page( 'edit-profile' )) ){
        // this should not be like this. Need to look into it.
        wp_deregister_style('wppb_stylesheet');
        wp_dequeue_style('wppb_stylesheet');
    }
 
    if( !is_singular( array('docs', 'post' ) ) ){
        wp_deregister_style('codebox');
        wp_dequeue_style('codebox');
    }
}
 
 
 
 
// list loaded assets by our theme and plugins so we know what we're dealing with. This is viewed by admin users only.
add_action('wp_print_footer_scripts', 'wra_list_assets', 900000);
function wra_list_assets(){
    if ( !current_user_can('delete_users') ){
        return;
    }
 
    echo '<h2>List of all scripts loaded on this particular page.</h2>';
    echo '<p>This can differ from page to page depending of what is loaded in that particular page.</p>';
 
    // Print all loaded Scripts (JS)
    global $wp_scripts;
    wra_print_assets($wp_scripts);
 
    echo '<h2>List of all css styles loaded on this particular page.</h2>';
    echo '<p>This can differ from page to page depending of what is loaded in that particular page.</p>';
    // Print all loaded Styles (CSS)
    global $wp_styles;
    wra_print_assets($wp_styles);
}
 
function wra_print_assets($wp_asset){
    $nb_of_asset = 0;
    foreach( $wp_asset->queue as $asset ) :
        $nb_of_asset ++;
        $asset_obj = $wp_asset->registered[$asset];
        wra_asset_template($asset_obj, $nb_of_asset);
    endforeach;
}
 
function wra_asset_template($asset_obj, $nb_of_asset){
    if( is_object( $asset_obj ) ){
        echo '<div class="wra_asset" style="padding: 2rem; font-size: 0.8rem; border-bottom: 1px solid #ccc;">';
 
        echo '<div class="wra_asset_nb"><span style="width: 150px; display: inline-block">Number:</span>';
        echo $nb_of_asset . '</div>';
 
 
        echo '<div class="wra_asset_handle"><span style="width: 150px; display: inline-block">Handle:</span>';
        echo $asset_obj->handle . '</div>';
 
        echo '<div class="wra_asset_src"><span style="width: 150px; display: inline-block">Source:</span>';
        echo $asset_obj->src . '</div>';
 
        echo '<div class="wra_asset_deps"><span style="width: 150px; display: inline-block">Dependencies:</span>';
        foreach( $asset_obj->deps as $deps){
            echo $deps . ' / ';
        }
        echo '</div>';
 
        echo '<div class="wra_asset_ver"><span style="width: 150px; display: inline-block">Version:</span>';
        echo $asset_obj->ver . '</div>';
 
        echo '</div>';
 
    }
}

+++++++++++++++++++++++++++++++++

Great post! Thanks for sharing.
I think “wp_deregister_script” is not needed, dequeue is enough and safer. You can also use “__return_false” function instead of making your own function which returns false.
Here is the script I use for my website metabox.io and it works:
<?php
// Remove styles of plugin
add_filter( 'jetpack_implode_frontend_css', '__return_false' );
add_filter( 'bbp_default_styles', '__return_empty_array' );
// Remove bbPress scripts on non-bbPress pages
add_filter( 'bbp_default_scripts', function ( $scripts )
{
return is_bbpress() ? $scripts : [ ];
} );
// Jetpack scripts
add_action( 'wp_enqueue_scripts', function ()
{
wp_dequeue_script( 'devicepx' );
}, 20 );
__________________________________________________________

add_action( 'wp_print_scripts', 'pp_deregister_javascript', 99 );
 
function pp_deregister_javascript() {
 wp_deregister_script( 'jquery-ui-datepicker' );
 wp_deregister_script( 'pp-del-avatar-script' );
 wp_deregister_script( 'pp-sweetalert2' );
 wp_deregister_script( 'pp-jcarousel' );
 wp_deregister_script( 'pp-bootstrap-filestyle' );
 wp_deregister_script( 'pp-chosen' );
}
 
add_action( 'wp_print_styles', 'pp_deregister_styles', 99 );
 
function pp_deregister_styles() {
 wp_deregister_style( 'font-awesome' );
 wp_deregister_style( 'ppcore' );
 wp_deregister_style( 'pp-social-button' );
 wp_deregister_style( 'pp-flat-ui' );
 wp_deregister_style( 'pp-bootstrap' );
 wp_deregister_style( 'pp-chosen' );
}
__________________________________________________________


FINAL OUTPUT

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
add_action( 'wp_print_scripts', 'pp_deregister_javascript', 99 );function pp_deregister_javascript() {    // FOR PRODUCTS    if ( is_product() ){        wp_dequeue_script( 'tp-tools' );        wp_dequeue_script( 'revmin' );        wp_dequeue_script( 'antiscroll' );        wp_dequeue_script( 'antiscroll' );          wp_dequeue_script( 'freeze_table' );          wp_dequeue_script( 'wcpt' );    }    // FOR PAGES - SPECIFIC ID PAGES     if ( is_page( array( 96, 'management' ) ) ){ //page 96 is the homepage        wp_dequeue_script( 'alg-wc-custom-payment-gateways' );    }  }
add_action( 'wp_print_styles', 'pp_deregister_styles', 99 );function pp_deregister_styles() {    // FOR PRODUCTS    if ( is_product() ){        wp_dequeue_style( 'sib-front-css' );        wp_dequeue_style( 'revslider-material-icons' );        wp_dequeue_style( 'revslider-basics-css' );        wp_dequeue_style( 'rs-color-picker-css' );        wp_dequeue_style( 'revbuilder-select2RS' );        wp_dequeue_style( 'rs-plugin-settings' );        wp_dequeue_style( 'antiscroll' );        wp_dequeue_style( 'freeze_table' );        wp_dequeue_style( 'wcpt' );        wp_dequeue_style( 'backwpup' );    }    // FOR PAGES    if ( is_page( array( "90,88" ) ) ){    wp_dequeue_style( '' );    }}
__________________________________________________________

Comments

Popular posts from this blog

How to fix WordPress 500 Internal Server Errorr after install or upload

When you move a WordPress instalation from one server to another, after you have all files in place, the database, and the database meta updates for the server address you must clear the PERMALINKS . Go to Permalinks -> select some option -> re select old options -> Click save . All pages should be working now . If you still have problems, check folder permissions or user server permissions with the apache service .

Remove Woocommerce Styles & Scripts on other pages

Remove WooCommerce styles and scripts. https://gist.github.com/gregrickaby/2846416   remove-woocommerce-styles-scripts.php <?php //* Do NOT include the opening php tag shown above. Copy the code shown below into functions.php /** * Manage WooCommerce styles and scripts. */ function grd_woocommerce_script_cleaner () { // Remove the generator tag remove_action ( 'wp_head' , array ( $ GLOBALS [ 'woocommerce' ], 'generator' ) ); // Unless we're in the store, remove all the cruft! if ( ! is_woocommerce () && ! is_cart () && ! is_checkout () ) { wp_dequeue_style ( 'woocommerce_frontend_styles' ); wp_dequeue_style ( 'woocommerce-general' ); wp_dequeue_style ( 'woocommerce-layout' ); wp_dequeue_style ( 'woocommerce-smallscreen' ); wp_dequeue_style ( 'woocommerce_fancybox_styles' ); wp_dequeue_style ( 'woocommerce_chosen_styles' ); wp_dequeue...