我正在开发WordPress插件,我有一个复选框选项来添加缓存控制头。如果选项设置为1,我想写入htaccess文件,否则删除新内容。
我一直在尝试使用insert_with_markers()
但无济于事。以下是我一直在使用的函数:
function write_cache_control(){
$htaccess = get_home_path().".htaccess";
$lines = array();
$lines[] = \'<FilesMatch "\\.(js|css|jpg|jpeg|gif|png|pdf|swf|svg|svgz|ico|ttf|ttc|otf|eot|woff|woff2|webp)$">\';
$lines[] = \'<IfModule mod_headers.c>\';
$lines[] = \'ExpiresActive On\';
$lines[] = \'ExpiresDefault "access plus 1 month"\';
$lines[] = \'Header set Cache-Control "public, immutable, max-age=2628000, s-maxage=2628000"\';
$lines[] = \'Header set Access-Control-Allow-Origin "*\';
$lines[] = \'</IfModule>\';
$lines[] = \'</FilesMatch>\';
insert_with_markers($htaccess, "Cache Control", $lines);
}
nbsp;function clear_cache_control(){
$htaccess = get_home_path().".htaccess";
insert_with_markers($htaccess, "Cache Control", \'\');
}
我试着把他们wp
, init
&;wp_init
但似乎什么都不管用。我有什么遗漏吗?我应该使用WordPress吗wp_rewrite
或PHP的fread
&;fwrite
还是类似的?