Skip to content

Instantly share code, notes, and snippets.

@neojp
Created March 21, 2012 18:50
Show Gist options
  • Save neojp/2151167 to your computer and use it in GitHub Desktop.
Save neojp/2151167 to your computer and use it in GitHub Desktop.
Enqueue WordPress static assets with dynamic versioning passed over in the query string.
// enqueue local theme assets
function _enqueue_assets($css, $js) {
// add files to the $css or $js arrays like this
// 'handle' => 'js/filename.js'
//
// or like this
// 'handle' => array(
// 'src' => 'js/filename.js', // don't start with /
// 'deps' => null, // can be string or array of strings
// 'version' => null,
// 'in_footer' => true,
// 'media' => 'screen'
// ),
$url = get_stylesheet_directory_uri();
$path = realpath(dirname(__FILE__) . '/../');
if (is_array($css) && count($css))
foreach ($css as $handle => $file) {
$file = is_array($file) ? $file : array('src' => $file);
if (!isset($file['src']) || !file_exists($path . '/' . $file['src']))
continue;
$filename = '/' . $file['src'];
$filepath = realpath($path . $filename);
$deps = isset($file['deps']) ? $file['deps'] : null;
$version = isset($file['version']) ? $file['version'] : filemtime($filepath);
$media = isset($file['media']) ? $file['media'] : 'screen';
wp_enqueue_style($handle, $url . $filename, $deps, $version, $media);
}
if (is_array($js) && count($js))
foreach ($js as $handle => $file) {
$file = is_array($file) ? $file : array('src' => $file);
if (!isset($file['src']) || !file_exists($path . '/' . $file['src']))
continue;
$filename = '/' . $file['src'];
$filepath = realpath($path . $filename);
$deps = isset($file['deps']) ? $file['deps'] : null;
$version = isset($file['version']) ? $file['version'] : filemtime($filepath);
$in_footer = isset($file['in_footer']) ? $file['in_footer'] : true;
wp_enqueue_script($handle, $url . $filename, $deps, $version, $in_footer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment