loading...

Automatically Removing Unused CSS from Wordpress Pages?

bennypowers profile image Benny Powers 🇮🇱🇨🇦 ・1 min read

The Problem

A client approached me with a WordPress site (100+ pages) that takes over 20 seconds to load. I analyzed the site with lighthouse and other tools and found that there's at least 2mb of useless CSS and other crap loading on the home page.

Looking into the WP setup, it uses an off-the-shelf (theme store) theme with the typical glut of useless crap.

The Naive Solution

I could go through each page, analyze the unused CSS with browser extensions, then collate the results of those analyses into a (hopefully smaller) theme style.css This would take hours of repetitive mechanical work, with lots of potential human error.

The Preferred Strategy

Tools like purifycss are able to statically analyze the final rendered markup for a page and strip the CSS file to only contain used rules. I think it should be very possible to write a plugin that does something like the following on each page load:

  1. Concat all CSS files together
  2. Replace all CSS links with the single concatted link
  3. Render the page
  4. Run purifycss on the rendered output, saving the minified and optimized CSS to a new file with a hashed name
  5. Replace the concatted link with a link to the new optimized file
  6. Serve the page

All of that should happen before the caching plugin.

The Question

Does something like this already exist? What about my plan, is it reasonable? Am I ignoring any potential gotchas?

Discussion

pic
Editor guide
Collapse
alexphelps profile image
Alex Phelps

This looks inline with your requirement -wordpress.org/plugins/psn-pagespee...

Google Pagespeed (for nginx) can do this as well and a more robust solution than a wp plugin though it requires more skills and access to setup.

Collapse
triviaquestion4 profile image
triviaquestions

hello friends, due to some CSS and images problem my blog speed is very low. My blog is related to quiz questions (triviaquestions4u.com/friends-triv...) and answers which contain simple content and images. but I don't know why it's too heavy. Anyone have a solution to this problem, please?

Collapse
adam_cyclones profile image
Adam Crockett

This might not be super helpful, but untie your thinking from the platform. This is not wordpress, this is a collection of files and folders. It is possible that there is a scanner to find unused rules sure but the unused rules might be tied to JavaScript logic. If it where me I'd write a simple puppeteer script and timebox it to strictly 2 hours. If that's not done, push back and explain that there is a serious problem, here is the report, what do you want to do about it? Then manually do the work. It's less risky that the end result won't mess up if you do this by hand.

Collapse
thkishor profile image
Thangjam Kishorchand

I use Asset clean up the plugin to remove unused CSS from my site and you can see the result unused-css.com/detect-unused-css?u... and they are also built with theme builder which lots of bloats that come with Elementor but fear not I have written a tutorial on how to remove unused CSS (manually) foxscribbler.com/remove-unused-css...

Collapse
hyperpress profile image
John Teague

One way to do this would be building an companion plugin for the Autoptimize plugin that runs the purifycss part after Autoptimize renders the concatenated output.css for pages and posts. The trick would be timing the trigger for purifycss. Maybe in a theme hook. Something for me to think about.

Collapse
orest22 profile image
Orest Hazda

Here is an interesting conversation discourse.roots.io/t/removing-unus...

Might help you.

Collapse
rahuldey12 profile image
Rahul Dey

Which plugins u r using in your site can u send screenshot

Collapse
rafaljosko profile image
Collapse
Sloan, the sloth mascot
Comment deleted
Collapse
onegoaloneaim profile image
ATN

My site cryptonerdz.com is having the same problem. It is very annoying.

Collapse
dgmhubindia profile image
dgmhubindia

unused css is autoptimizer how can i fix that problem
please help me to solve this. my website is dgm hub

Collapse
khaledsemada profile image
Collapse
zuzukitap profile image
ZuzuKitap

My web site duzelti.net is same problem.

Collapse
amatosoft profile image
amatosoft

how can i fix that problem help me to solve this.
My website Digital marketing company in Kochi

Collapse
lapduc profile image
Info Comment marked as low quality/non-constructive by the community. View code of conduct
lapduc

unused css is a headache problem with me. I use wordpress website with flatsome theme, i tested it with google pagespeed in 3 month, result is bad, google say my website too slow. Now i know how to fix it. i will try clean my css hand html code. My website about used forklift in VietNam .Thanks