When developing a WordPress theme, we often integrate with plugins to save time or add features. Many plugins load their own JavaScript and CSS files on the front end. If those assets are already bundled into the theme, or if the plugin loads files you do not need, keeping the originals can be wasteful.
From a performance perspective, removing unnecessary assets can reduce HTTP requests and help avoid CSS or JavaScript conflicts with your theme.
How WordPress registers front-end assets
Deregistering is just the reverse of registering. The handle names used during registration are the same names you will later need when you remove the files.
add_action('wp_enqueue_scripts', 'register_plugin_styles');
function register_plugin_styles() {
wp_register_style('wizhi-style', plugins_url('my-plugin/css/plugin.css'));
wp_enqueue_style('wizhi-style');
}
Deregister a plugin’s JavaScript and CSS files
Once you know the asset handle, you can remove the file with wp_deregister_style() or wp_deregister_script(). The easiest way to find the correct handle is to search the plugin source code for wp_enqueue_style and wp_enqueue_script.
add_action('wp_print_styles', 'wizhi_print_css');
function wizhi_print_css() {
wp_deregister_style('wizhi-style');
}
add_action('wp_print_scripts', 'wizhi_print_scripts');
function wizhi_print_scripts() {
wp_deregister_script('wizhi-script');
}
Removing unneeded files can lower the number of front-end requests and reduce the chance that a plugin’s CSS or JavaScript will conflict with your theme. It is a simple technique, but it makes a real difference in polished theme development.
