Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created October 27, 2010 04:32
Show Gist options
  • Save jboesch/648439 to your computer and use it in GitHub Desktop.
Save jboesch/648439 to your computer and use it in GitHub Desktop.
<?
/**
* Some versioned URL's for cache busting when we push new changes up
* Only need the file name, paths will resolve appropriately in the
* $file_path variable
* Usage: <?= vurl('myfile.js'); ?>
*
* @param $file {String} The file - eg. 'file.js' or 'file.css'
* @param $type {String} We can optionally specify a type (probably not needed)
*/
function vurl($file, $type = ''){
$root = get_bloginfo('template_directory');
$template_path = TEMPLATEPATH;
// We haven't specified a type, get the file extension to determine type
if(!$type){
$type = end(explode('.', $file));
}
// In this case, our path's are auto set to mysite.com/wp-content/themes/mytheme/{type}/{file}
$file_path = '/' . $type . '/' . $file;
$abs_file = $template_path . $file_path;
$types_tags = array(
'js' => '<script type="text/javascript" src="%s"></script>',
'css' => '<link rel="stylesheet" type="text/css" href="%s" />'
);
if(!file_exists($abs_file) || !isset($types_tags[$type])){
return false;
}
$cache_qs = '?' . filemtime($abs_file);
$tag_out = sprintf($types_tags[$type], $root . $file_path . $cache_qs) . "\n";
return $tag_out;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment