In my years of optimizing WordPress websites, one of the most overlooked and effective way to improve site speed is removing unused CSS and JavaScript. Unused CSS and JavaScript files clog your site’s performance, increase page load times, and negatively affect user experience. This article provides a step-by-step guide to identifying and removing unused CSS and JavaScript in WordPress, based on my hands-on experience with client sites. I’ll cover tools, manual techniques, and plugins, ensuring your site runs lean and fast.
Why Removing Unused CSS and JavaScript Matters
Unused CSS and JavaScript refers to code that loads on your WordPress site but isn’t contribute to rendering or functionality. For example, a plugin might enqueue styles for every page, even when unused on most. This bloat impacts:
- Page Load Time: Extra files increase HTTP requests and file sizes.
- SEO: Google’s Core Web Vitals penalize slow-loading sites.
- User Experience: Faster sites reduce bounce rates.
A 2023 study by Google showed that a 1-second delay in load time can reduce conversions by 7%. Removing unused CSS and JavaScript is a practical way to optimize performance.
Step 1: Identify Unused CSS and JavaScript
Before removing unused CSS and JavaScript, you need to know what’s unnecessary. Here are tools I rely on:
Tool | Purpose | FreeFree/Paid | Link |
---|---|---|---|
Chrome DevTools | Analyzes CSS/JavaScript coverage | Free | Google Chrome |
Asset CleanUp | Disables unused scripts/styles per page | Free/Pro | Asset CleanUp |
PurifyCSS | Detects unused CSS | Free (CLI) | PurifyCSS GitHub |
GTmetrix | Reports unused code impact | Free/Pro | GTmetrix |
Using Chrome DevTools
- Open your site in Chrome.
- Press Ctrl + Shift + I to open DevTools.
- Go to the Coverage tab.
- Click the record button, reload the page, and view unused CSS/JavaScript (highlighted in red).
This method helped me identify a client’s theme loading 200KB of unused CSS on their homepage.
Step 2: Remove Unused CSS and JavaScript with Plugins
Plugins simplify the process for non-developers. Here are two I’ve used successfully:
Asset CleanUp
This plugin lets you disable CSS/JavaScript files on specific pages. For a client’s e-commerce site, I used it to stop WooCommerce scripts from loading on blog pages, reducing load time by 30%.
Steps:
- Install Asset CleanUp.
- Go to CSS/JavaScript Manager in your WordPress dashboard.
- Select a page and uncheck unused files.
- Test thoroughly to ensure functionality isn’t broken.
Perfmatters
Perfmatters offers advanced script management. I’ve used it to defer or remove unused JavaScript, like jQuery, on lightweight landing pages.
Steps:
- Install Perfmatters.
- Navigate to Assets > Script Manager.
- Disable scripts by page or post type.
PluginPlugin | Key Feature | Price |
---|---|---|
Asset CleanUp | Per-page script control | Free/$49 Pro |
Perfmatters | Advanced defer/remove options | $24.95/year |
Step 3: Manually Remove Unused CSS and JavaScript
For developers, manual removal offers precision. Here’s how I approach it:
Removing Unused CSS
- Identify: Use PurifyCSS or Chrome DevTools to find unused rules.
- Edit Theme Files: In your child theme’s style.css, remove unused styles.
- Use Critical CSS: Extract critical CSS for above-the-fold content and inline it. Tools like Critical automate this.
Example:
/* Before */
.header, .footer, .unused-class {
display: none;
}
/* After */
.header, .footer {
display: none;
}
Removing Unused JavaScript
- Deregister Scripts: Use wp_deregister_script() in your child theme’s functions.php.
- Example: To remove a plugin’s script:
function remove_unused_js() { if (!is_page('contact')) { wp_deregister_script('contact-form-js'); } } add_action('wp_enqueue_scripts', 'remove_unused_js', 100);
I once reduced a site’s JavaScript payload by 150KB by deregistering unused plugin scripts.
Step 4: Optimize Remaining CSS and JavaScript
After removing unused code, optimize what’s left:
- Minify Files: Use plugins like Autoptimize to compress CSS/JavaScript.
- Defer JavaScript: Add defer to script tags or use Perfmatters.
- Combine Files: Reduce HTTP requests by combining CSS/JavaScript files (test carefully).
Fixing Slow WordPress Dashboards with a Lean Admin Tweaks Guide
Step 5: Test Your Site
Removing unused CSS and JavaScript can break functionality if done carelessly. I always:
- Test on a staging site first.
- Check all pages, especially dynamic ones (e.g., WooCommerce checkout).
- Use GTmetrix to confirm performance improvements.
For a client’s portfolio site, this process cut load time from 3.2 seconds to 1.1 seconds.
Common Mistakes to Avoid
- Removing Critical Scripts: Disabling jQuery might break plugins.
- Over-Optimization: Combining all files can cause conflicts.
- No Backups: Always back up before making changes.
Conclusion
Removing unused CSS and JavaScript in WordPress is a proven way to boost site speed and SEO. Whether using plugins like Asset CleanUp or manual methods, the key is careful identification, removal, and testing. In my experience, this process can shave seconds off load times, delighting users and search engines alike.