How to Disable CSS and Javascript Files in WordPress
One of our esteem plugin buyers asked us via email if it was possible to disable some or all CSS and JavaScript files added by ProfilePress in his WordPress powered website thus, the inspiration of this post.
Before you can remove or disable CSS and JavaScript files in WordPress, their handles has to be known.
In ProfilePress for example, the core CSS and javascript files are added or rather enqueue to WordPress using ppress-frontend
and ppress-frontend-script
respectively. See code below.
wp_enqueue_style('ppress-frontend', PPRESS_ASSETS_URL . "/css/frontend{$suffix}.css", false, PPRESS_VERSION_NUMBER);
wp_enqueue_script('ppress-frontend-script', PPRESS_ASSETS_URL . "/js/frontend{$suffix}.js", ['jquery', 'ppress-flatpickr'], PPRESS_VERSION_NUMBER, true);
Say we want to disable the above style and script from rendering when WordPress is loaded, the wp_deregister_style()
and wp_deregister_script()
functions are employed like so:
add_action( 'wp_print_scripts', 'pp_deregister_javascript', 99 );
function pp_deregister_javascript() {
wp_deregister_script( 'ppress-frontend-script' );
}
add_action( 'wp_print_styles', 'pp_deregister_styles', 99 );
function pp_deregister_styles() {
wp_deregister_style( 'ppress-frontend' );
}
You can disable multiple scripts and style as follows without having to create PHP functions for each CSS and JavaScript component.
add_action( 'wp_print_scripts', 'pp_deregister_javascript', 99 );
function pp_deregister_javascript() {
wp_deregister_script( 'ppress-flatpickr' );
wp_deregister_script( 'ppress-select2' );
wp_deregister_script( 'ppress-frontend-script' );
wp_deregister_script( 'ppress-member-directory' );
wp_deregister_script( 'password-strength-meter' );
}
add_action( 'wp_print_styles', 'pp_deregister_styles', 99 );
function pp_deregister_styles() {
wp_deregister_style( 'ppress-frontend' );
wp_deregister_style( 'ppress-flatpickr' );
wp_deregister_style( 'ppress-select2' );
}
FYI: the code above actually removes all CSS and JavaScript files added to WordPress by ProfilePress plugin 😀
La Fin!