Retriev3r
8/13/2016 - 11:32 AM

Add a query string to the end of the theme's style.css when WordPress is loaded.

Add a query string to the end of the theme's style.css when WordPress is loaded.

// Enqueue styles
function lernenleben_load_style() {
		if ( !is_admin() ) {
		  $cssfile = get_stylesheet_directory() . "/style.css";
	      $turi = get_template_directory_uri();
	      if (file_exists($cssfile)) {
	        $cssuri = "$turi/style.css?v=" . filemtime($cssfile);
	      }
	    wp_enqueue_style( 'lernenleben_googleFonts', '//fonts.googleapis.com/css?family=Lato:100,300,400,700|Dancing+Script' );
	    wp_enqueue_style( 'lernenleben_style', $cssuri );
	    wp_enqueue_style( 'su-box-shortcodes-css', site_url() .'/wp-content/plugins/shortcodes-ultimate/assets/css/box-shortcodes.css?ver=4.9.9' );
	}
}
<?php

/**
 * Add a filter to stylesheet_uri, which appends a query string to it automagically.
 */
add_filter('stylesheet_uri', 'zk_css_versioner');

/**
 * the goal of this method is to append a query string to the css url for the site.
 * the query string currently is determined by the last time the css file was modified
 * the theory is that if the file is modified, change the value of v=, which should
 * force the CDN to pull a new version from the origin server. This could be a
 * a hit on performance, not sure yet. Let's get it working first. 
 * 
 * @return string
 */
function zk_css_versioner() {


    $cssfile = get_stylesheet_directory() . "/style.css";
    $turi = get_template_directory_uri();

    if (file_exists($cssfile)) {
        $cssuri = "$turi/style.css?v=" . filemtime($cssfile);
    }

    echo $cssuri;
}

?>