Detect if style.min.css Exists in WordPress

Here’s a handy little script that will check if your theme has a minified stylesheet, and if so, use it instead of the main stylesheet.

// if a style.min.css file exists, use that, other use style.css
function style_or_min_style() {
$located = locate_template( 'style.min.css' );
if ($located != '' ) {
echo '';
} else {
echo '';

Then add this to your header.php file:

<?php style_or_min_style(); ?>

Or hook it directly into wp_head by adding this to functions.php (don’t use the function call above if you do it this way):

add_action( 'wp_head', 'style_or_min_style');

Why Would I Want to Do This?

Well, minifying your stylesheet is one of the recommendations of Google’s Pagespeed, as it is yet another small piece of the puzzle toward getting your site to run as leanly as possible. There are plugins out there that will do this for you, but if you’re the hands-on type, this method will allow you a bit more control.

I use to minimize my stylesheets.

How it Works

You’ll need to make sure there is a file called style.min.css in your theme’s directory, and it should be the minified version of your regular style.css file.

If you then want to edit style.css, you can go ahead and do that, or troubleshoot by quickly renaming the style.min.css file to anything else (ie, style1.min.css).

Up Next: TinyLetter WordPress Plugin