admin管理员组文章数量:1026989
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?
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?
1 Answer
Reset to default 2Almost all of your code is correct. I didn't even know about the insert_with_markers function, and now I do, so thank you.
In return, I believe I have your solution.
get_home_path()
is not defined when any of the hooks (init
,wp
, etc.) you mentioned are triggered. That function is defined in the admin-side "file.php", which as part of the admin dashboard side of things, has not yet loaded.
The hook admin_init
, however, should serve you well, presuming you are making those .htaccess changes on the admin side. If not, you could either include wp-admin/includes/file.php manually, or use an alternate way of finding .htaccess, such as the constant ABSPATH (although they are not equivalent).
Using the admin_init hook on the first function you provided, I found the rules written to my .htaccess file the next time I loaded a page on the admin side.
By the way, you might want to do a check for current_user_can( 'edit_files' )
before allowing that code to run.
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?
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?
1 Answer
Reset to default 2Almost all of your code is correct. I didn't even know about the insert_with_markers function, and now I do, so thank you.
In return, I believe I have your solution.
get_home_path()
is not defined when any of the hooks (init
,wp
, etc.) you mentioned are triggered. That function is defined in the admin-side "file.php", which as part of the admin dashboard side of things, has not yet loaded.
The hook admin_init
, however, should serve you well, presuming you are making those .htaccess changes on the admin side. If not, you could either include wp-admin/includes/file.php manually, or use an alternate way of finding .htaccess, such as the constant ABSPATH (although they are not equivalent).
Using the admin_init hook on the first function you provided, I found the rules written to my .htaccess file the next time I loaded a page on the admin side.
By the way, you might want to do a check for current_user_can( 'edit_files' )
before allowing that code to run.
本文标签: phpWrite toremove from default htaccess file from plugin
版权声明:本文标题:php - Write toremove from default .htaccess file from plugin? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655551a2161578.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论