I'm working on a WordPress plugin, and I have a checkbox option to add cache control headers. I'd like to write to the htaccess file if the option is set to 1, else remove the new content.
I've been trying to achieve this using insert_with_markers()
but to no avail. These are the functions I've been using:
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);
}
function clear_cache_control(){
$htaccess = get_home_path().".htaccess";
insert_with_markers($htaccess, "Cache Control", '');
}
I've tried hooking them into wp
, init
& wp_init
but nothing seems to work. Is there something I'm missing? Should I be using WordPress' wp_rewrite
or PHP's fread
& fwrite
or something along those lines?