WordPress Guide – Disable CSS and JavaScript

In WordPress, you may want to disable CSS and JavaScript files for various reasons, such as improving site speed or troubleshooting conflicts with plugins or themes. Here are a few methods for disabling CSS and JavaScript in WordPress:

  1. Using a Plugin: One of the easiest ways to disable CSS and JavaScript files in WordPress is by using a plugin. There are several plugins available that can help you disable specific CSS and JavaScript files or even all CSS and JavaScript files. One popular plugin is Asset Cleanup Pro, which allows you to selectively disable CSS and JavaScript files on specific pages or site-wide.
  2. Code Snippets: Another option is to use code snippets to disable CSS and JavaScript files. Here are some examples of code snippets you can use:

To disable a specific CSS file, add the following code snippet to your functions.php file:

php
function wpse_dequeue_styles() {
wp_dequeue_style( 'style-name' );
}
add_action( 'wp_print_styles', 'wpse_dequeue_styles', 9999 );

To disable a specific JavaScript file, add the following code snippet to your functions.php file:

php
function wpse_dequeue_scripts() {
wp_dequeue_script( 'script-name' );
}
add_action( 'wp_print_scripts', 'wpse_dequeue_scripts', 9999 );

To disable all CSS and JavaScript files, add the following code snippet to your functions.php file:

php
function wpse_disable_all_scripts_and_styles() {
wp_dequeue_script( 'jquery' );
wp_dequeue_style( 'style' );
wp_dequeue_style( 'dashicons' );
}
add_action( 'wp_enqueue_scripts', 'wpse_disable_all_scripts_and_styles', 9999 );
  1. Use a Child Theme: If you want to disable CSS or JavaScript files that are included in your parent theme, you can create a child theme and remove the calls to those files from your child theme’s functions.php file. This will prevent those files from being loaded on your site.

In general, it’s important to be cautious when disabling CSS and JavaScript files, as it can have unintended consequences. It’s a good idea to test any changes in a staging environment before implementing them on your live site.