Menu Close

How to disable all WordPress Styles dan Scripts

Sometimes we want to  check our WordPress site speed, without load any script or style. So this is the correct code to add in functions.php

Remove All Scripts

function pj_remove_all_scripts() {
   foreach( wp_scripts()->registered as $script ) {
      wp_dequeue_script( $script->handle );
      wp_deregister_script( $script->handle );
  }
}
add_action('wp_enqueue_scripts', 'pj_remove_all_scripts', 998);

Remove All Styles

function pj_remove_all_styles() {
   foreach( wp_styles()->registered as $style ) {
      wp_dequeue_style( $style->handle );
      wp_deregister_style( $style->handle );
  }
}
add_action('wp_enqueue_scripts', 'pj_remove_all_styles', 998);

Remove All Scripts on certain page or post

function pj_remove_all_scripts() {
   foreach( wp_scripts()->registered as $script ) {
      if( is_page(array( 42, 'about-me', 'Contact' )) || is_single(array(17,'beef-stew','Irish Stew')) ) {
         wp_dequeue_script( $script->handle );
         wp_deregister_script( $script->handle );
      }
  }
}
add_action('wp_enqueue_scripts', 'pj_remove_all_scripts', 998);

You can add any conditional WordPress function, is_page(), is_single() or is_singular().

If your site still slow even after deregister all styles and scripts. Next thing you need to check is your query.
I suggest you to use Query Monitor plugins

Leave a Reply

Your email address will not be published. Required fields are marked *