这是我从一个未发布的CDN插件中获得的代码。它将自动替换排队函数使用的ver query\\u var和文件时间:
class CDN_VersionAssets {
private $default_version = \'\';
private $root_url;
public function __construct() {
$this->root_url = site_url();
}
public function initialize() {
add_filter(\'style_loader_src\', array($this, \'replace_version\'), 10);
add_filter(\'script_loader_src\', array($this, \'replace_version\'), 10);
add_filter(\'stylesheet_uri\', array($this, \'replace_version\'), 10);
}
public function on_template_redirect() {
$this->default_version = @filemtime(get_stylesheet_directory().\'/style.css\');
}
private function get_version($url) {
if(0 === strpos($url, $this->root_url)) {
$parts = parse_url($url);
$file_path = str_replace(site_url(\'/\'), ABSPATH, $parts[\'scheme\'].\'://\'.$parts[\'host\'].$parts[\'path\']);
if( !($version = @filemtime($file_path)) ) {
$version = $this->default_version;
}
return $version;
}
return false;
}
public function replace_version($src) {
if( $new_version = $this->get_version($src) ) {
return add_query_arg(\'ver\', $new_version, $src);
}
return $src;
}
}
add_action(\'init\', array(new CDN_VersionAssets(), \'initialize\'));