Skip to content

Instantly share code, notes, and snippets.

@gildotdev
Created December 18, 2013 03:10
Show Gist options
  • Save gildotdev/8016692 to your computer and use it in GitHub Desktop.
Save gildotdev/8016692 to your computer and use it in GitHub Desktop.
Drupal 7 hook method to remove cache busting query strings on css and js files. Related to this post https://drupal.org/node/242875
<?php
/**
* Implements hook_process_html
*/
function templatenamespace_process_html(&$vars) {
$search = array('scripts' => 'src=', 'styles' => 'href=', 'styles' => '@import\surl\(');
foreach ( $search as $var => $word ) {
if ( !empty($vars[$var]) ) {
$lines = explode("\n", $vars[$var]);
$result = array();
foreach($lines as $line) {
$matches = array();
if ( preg_match('/' . $word . '"(.*)"/', $line, $matches) ) {
global $language;
$match = $matches[1];
$replacement = $matches[1];
// remove the ? and everything behind it
$pos = strpos($replacement, '?');
$replaced = $line;
if ( $pos !== FALSE ) {
$replacement = substr($replacement, 0, $pos);
$replaced = str_ireplace($match, $replacement, $line);
}
$result[] = $replaced;
}
else {
$result[] = $line;
}
}
if ( !empty($result) ) {
$vars[$var] = implode("\n", $result);
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment