Include custom.css with Last-Modified Timestamp Token Automatically

With this mod, you can have your own custom.css included in both desktop and mobile sites automatically. When the file is modified, the last modified timestamp is used as a token to prevent out-dated CSS cache being used.

In /includes/display/HTMLHead.php

Find

foreach($stylesheets as $stylesheet) {
	if(empty($stylesheet['media'])) {
		$stylesheet['media'] = 'all';
	}

	// Add caching token
	if(strpos($stylesheet['stylesheet'], '?') === false) {
		$stylesheet['stylesheet'] .= '?';
	}
	else {
		$stylesheet['stylesheet'] .= '&';
	}
	$stylesheet['stylesheet'] .= getConfig('JSCacheToken');

	$link = '<link href="'.$styleRoot.'/'.$stylesheet['stylesheet'].'" media="'.$stylesheet['media'].'" type="text/css" rel="stylesheet" />';
	if(!empty($stylesheet['condition'])) {
		$link = '<!--[if '.$stylesheet['condition'].']>'.$link.'<![endif]-->';
	}
	$GLOBALS['Stylesheets'] .= $link."\n";
}

Replace

$csstoken = filemtime($_SERVER['DOCUMENT_ROOT'].'/templates/custom.css');
foreach($stylesheets as $stylesheet) {
	if(empty($stylesheet['media'])) {
		$stylesheet['media'] = 'all';
	}
	$link = '<link href="'.$styleRoot.'/'.$stylesheet['stylesheet'].'?'.$csstoken.'" rel="stylesheet">';
	if(!empty($stylesheet['condition'])) {
		$link = '<!--[if '.$stylesheet['condition'].']>'.$link.'<![endif]-->';
	}
	$GLOBALS['Stylesheets'] .= $link."\n";
}
$GLOBALS['Stylesheets'] .= '<link href="/templates/custom.css?'.$csstoken.'" rel="stylesheet">';

Leave a Reply

Your email address will not be published. Required fields are marked *