我试图在插件激活期间在.htaccess中添加重写规则。如何在插件停用时编辑/回滚.htaccess中的重写规则?
function add_to_htaccess( $rules ) {
$content = <<<EOD
\nAddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml
SetOutputFilter DEFLATE\n\n
EOD;
return $content . $rules;
}
add_filter('mod_rewrite_rules', 'add_to_htaccess');
function myplugin_enable_flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
register_activation_hook( __FILE__, 'myplugin_enable_flush_rules' );
上述函数在默认WordPress规则中添加了重写规则,位于# BEGIN WordPress
和# END WordPress
之间。我该怎么做才能单独添加(在.htaccess的底部?)
答案 0 :(得分:0)
我已经设法自己做了。以下是我添加的功能。
function myplugin_deactivate() {
// This will remove the rewrite rules
remove_filter('mod_rewrite_rules', 'add_to_htaccess');
global $wp_rewrite;
// Flush the rewrite rules
$wp_rewrite->flush_rules();
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );