WordPress is fun, but many of my clients take the plugin fun to new levels :)
Depending on how well plugins / shortcodes have been integrated, linked JavaScript & CSS files can clutter up your pages really fast.
The problem is that many plugins do not load external files on demand. Same applies for WebFonts that have been added to the system.
Its no fun hunting down all those resources and slim down pages manually, but sometimes there is no way around it.
In WordPress CSS & JavaScript files are enqueued by plugins / themes.
wp_enqueue_style( 'my-extension', get_template_directory_uri() . '/css/my-extension.css', '1.2', // version number 'screen', // CSS media type );
wp_enqueue_script( 'my-extension-script', get_template_directory_uri() . '/js/my-extension.js', '1.0.0', true // Place in footer );
To remove files from pages you need to unregister (CSS / JavaScript) those files using their specific handle. This also allows you to replace a specific file with your own version.
wp_deregister_style( 'my-extension' ); wp_deregister_script( 'my-extension-script' );
The big problem is finding the names of the enqueued files, their handles. They are often hidden deep in the plugins. But if they are registered somewhere, you should be able to find them somewhere :)
global $wp_styles;
Add this to your functions.php and you will get a nice overview of all the styles and their handles registered. You can do the same for the registered scripts.
global $wp_scripts;
This will get you an overview of all the registered files. Now its only a matter of doing the unregister operation with some simple WordPress conditional logic.
add_action( 'some-extension', 'some_mextension', 100 ); function my_extension() { if ( !is_page('Home') ) { wp_deregister_script( 'some-extension' ); } }
Yes there is and it is called “Asset Queue Manager” …
“This tool allows you to monitor, dequeue and requeue scripts and styles that are enqueued on your site. It is designed for frontend performance engineers who want to view and manage all assets enqueued on any page and control the minification and concatenation themselves.”
Messing with styles can be ugly, but often wont break things completely. Messing with JavaScript includes can cripple a setup badly. So you should be really sure which scripts / styles can be deactivated on specific pages. Please do not experiment on a live website … I warned you ! :)
BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More
THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More
2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More
Short look back at 2023 This has been a busy and interesting year. I am… Read More
Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More
Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More