Categories: DevelopmentWordPress

Remove WordPress CSS / JS / Fonts website clutter

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.

THE HUNT

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.

THE QUEUE

CSSJavaScript

  1. wp_enqueue_style(
  2.     'my-extension',
  3.     get_template_directory_uri() . '/css/my-extension.css',
  4.     array( 'bootstrap' ),// an array of dependent styles  
  5.     '1.2', // version number
  6.     'screen', // CSS media type
  7. );
  1. wp_enqueue_script( 'my-extension-script',
  2. get_template_directory_uri() . '/js/my-extension.js',
  3. array(), // Scripts your extension depends on
  4. '1.0.0',
  5. true // Place in footer
  6. );

CLEANUP

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.

  1. wp_deregister_style( 'my-extension' );
  2.  
  3. wp_deregister_script( 'my-extension-script' );

DO YOU HAVE A NAME?

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 :)

  1. global $wp_styles;
  2. print_r($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.

  1. global $wp_scripts;
  2. print_r($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.

  1. add_action( 'some-extension', 'some_mextension', 100 );
  2. function my_extension() {
  3. if ( !is_page('Home') ) {
  4. wp_deregister_script( 'some-extension' );
  5. }
  6. }

IS THERE A PLUGIN FOR THAT?

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.”

BREAKING STUFF

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 ! :)

… enjoy coding

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago