• Resolved Joost Schuur

    (@jschuur)


    I’m using the Amazon CloudFront CDN to distribute some of the content on my site (images, CSS, JavaScript), and due to the way files are propagated and updates, my site style sheet has a date based file name (e.g. style_08022010.1.css).If I were just style.css, any changes I make to it could take up to 24 hours to propagate.

    This means that I updated my header.php to load this new style sheet instead and have no more need for a style.css. That is, until I realized that the absence of a style.css causes WordPress to think a theme is broken, and if you go to the theme selector page or editor e.g., it realizes this and will disable it, saying ‘Stylesheet is missing’. So I needed one after all.

    My question evolves around the best way to handle this situation, with the least amount of work required whenever a style sheet is renamed.

    At first I thought I’d simply add a symlink from style.css to the actual style sheet, but that would have to be updated every time I push out a new style sheet change on top of editing header.php. You can’t have a blank style.css either, since the comments in the header of it need to contain the style sheet name/description/tags etc. I could add an @import rule to the original style sheet, but then that has to be updated every time too.

    Ideally, I don’t want to even change the header. I’d love to have some kind of succinct way in PHP to say ‘include the first file matching ‘style_*.css‘, but you need to use opendir/readir for that and it’s a lot of overkill.

    So right now, I have a static style.css with no @import rule and just the standard fields in the header comment. It doesn’t get loaded by anything from the site and exists only to keep WordPress happy. Whenever I push out a new style sheet change, I rename the old one and modify header.php

    I did briefly look into using the stylesheet_uri filter to dynamically override what style sheet is loaded, but WordPress is still hard coded to look for style.css or it thinks a theme is broken.

    Looking forward to hearing anyone else’s thoughts on this.

Viewing 5 replies - 1 through 5 (of 5 total)
  • DigitalSquid

    (@twelvefootsnowman)

    If the stylesheet name is going to be constantly changing and you don’t want to have to manually update and files when it does, then your only real option is to use a function to search for the latest version of the file.

    Apart from opendir/readir, the only other PHP option I can think of is glob() to get all the names of .css files and then echoing out the highest one in your header.php

    Thread Starter Joost Schuur

    (@jschuur)

    Thanks for the tip. Here’s what I ended up doing, with a style sheet naming convention of style_YYYMMDD.V.css:

    function get_latest_stylesheet() {
    	$stylesheet = "style.css";
    
    	foreach (glob(get_stylesheet_directory() . "/style_*.css") as $filename) {
    		$filename = basename($filename);
    		if ($filename > $stylesheet)
    			$stylesheet = $filename;
    	}
    
    	return dirname(get_stylesheet_uri()) . "/" . $stylesheet;
    }

    It defaults to style.css and is overridden by the most recently named style sheet. My header then just uses

    <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_latest_stylesheet() ?>" />

    Since the site uses caching for its content, and there’s usually only one style sheet anyway, I’m not worried about a performance impact.

    Thread Starter Joost Schuur

    (@jschuur)

    Just for fun, I could do the following to use the transient cache to save repeated hits to the file system for non static page access:

    function get_latest_stylesheet() {
    
    	if (false == ($stylesheet = get_transient("ttidg_latest_stylesheet"))) {
    		$stylesheet = "style.css";
    
    		foreach (glob(get_stylesheet_directory() . "/style_*.css") as $filename) {
    			$filename = basename($filename);
    			if ($filename > $stylesheet)
    				$stylesheet = $filename;
    		}
    
    		$stylesheet = dirname(get_stylesheet_uri()) . "/" . $stylesheet;
    
    		// Cache the value for an hour.
    		set_transient("ttidg_latest_stylesheet", $stylesheet, 3600);
    	}
    
    	return $stylesheet;
    
    }

    But now I have to delete the cache when I upload a new style sheet. Not a huge problem, I can automate a delete_transient() call in my deployment process too (I use git to push/pull theme updates).

    Just figured I’d share this if someone stumbles upon this thread.

    I recommend W3 Total Cache for you all, it does all of these things automatically/transparently.

    Joost, thanks for sharing this. Worked out great for me on a project that I’m working on.

    Frederick, I’ll check out W3 Total Cache next time we need caching. In this case, the client was already using WP Super Cache and they push an insane amount of bandwidth so a change midstream wasn’t really an option.

    Merry Christmas guys!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Strategy for style sheets with date in the file name’ is closed to new replies.