在WP插件激活时加载.htaccess更改

时间:2018-11-27 16:48:05

标签: php wordpress apache .htaccess

我正在开发一个WordPress插件,在其中我对.htaccess文件进行了更改,并且我想删除停用插件时的更改,并且想要恢复有关激活插件时的更改。

我所做的更改是使用mod_rewrite_rules挂钩添加生成的文本块。

刷新有关插件停用的规则工作正常,但是在激活时我无法获取它来添加新块。

在某些班上:

public function __construct() {
    add_filter('mod_rewrite_rules', array($this, 'generate'));
}

public function generate( $existing_htaccess ) {
    // If the plugin is not active, do not modify .htaccess contents.
    if ( '0' === get_option('myplugin_active') ) {
        return $existing_htaccess;
    }

    // Generate changes.
    $changes = PHP_EOL . '# made some changes here' . PHP_EOL . PHP_EOL;

    return $changes . $existing_htaccess;
}

在我的插件引导文件中:

function myplugin_activate() {
    update_option('myplugin_active', '1');
    flush_rewrite_rules();
}

function myplugin_deactivate() {
    update_option('myplugin_active', '0');
    flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'myplugin_activate' );
register_deactivation_hook( __FILE__ , 'myplugin_deactivate' );

0 个答案:

没有答案