加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在WordPress插件中以编程方式添加Mod重写规则

发布时间:2020-12-13 18:05:06 所属栏目:PHP教程 来源:网络整理
导读:当用户更改固定链接设置时,以下示例插件将自定义mod重写规则添加到.htaccess. /* Plugin Name: Sample Mod Rewrite */add_action('generate_rewrite_rules',array(new custom_mod_rewrite,"generate_rewrite_rules"));class custom_mod_rewrite { function _
当用户更改固定链接设置时,以下示例插件将自定义mod重写规则添加到.htaccess.
/* Plugin Name: Sample Mod Rewrite  */

add_action('generate_rewrite_rules',array(new custom_mod_rewrite,"generate_rewrite_rules"));

class custom_mod_rewrite {
    function __construct() {
        $this->wp_rewrite = & $GLOBALS["wp_rewrite"];
    }
    function generate_rewrite_rules() {

        $non_wp_rules = array(
            'simple-redirect/?$plugin_name' => 'http://google.com','one-more-redirect/?$plugin_name' => 'http://yahoo.com'
        );

        $this->wp_rewrite->non_wp_rules = $non_wp_rules + $this->wp_rewrite->non_wp_rules;
        add_filter('mod_rewrite_rules',array(&$this,"mod_rewrite_rules"));
    }
    function mod_rewrite_rules($rules) {
        return preg_replace('#^(RewriteRule ^.*/)?$plugin_name .*(http://.*) [QSA,L]#mi','$1 $2 [R=301,L]',$rules);
    }
}

我发现这有两个问题.

>如果将其设置为默认永久链接,则不会添加规则.
>更重要的是,除非用户更改固定链接设置,否则不会添加规则. (可以通过在插件激活时执行的$wp_rewrite-> flush_rules()来解决)

对于#2,我想知道是否有一种以编程方式添加规则的好方法.

IIS (common on Windows servers) does not support mod_rewrite.

来源:http://codex.wordpress.org/Using_Permalinks#Permalinks_without_mod_rewrite

听起来并非所有系统都使用.htaccess.因此,直接编辑.htaccess文件可能不是分布式插件的最佳选择.我不知道.可能我必须检查服务器是否使用Apache,如果需要,我需要检查.htacess是否可写并且现有规则没有添加规则,最后我可以将规则附加到它.此外,当用户停用插件时,必须删除规则.所以这很麻烦.

如果WordPress可以使用内置的API或其他东西来处理它,我想把它留给WordPress.但上面的例子是我迄今为止所能找到的.所以我感谢您的信息.

更新

正如pfefferle建议的那样,我可以使用$wp_rewrite-> flush_rules().然而,问题#1仍然存在;使用默认永久链接设置时,它不会产生任何影响.有任何想法吗?

/* Plugin Name: Sample Mod Rewrite  */

$custom_mod_rewrite = new custom_mod_rewrite;
register_activation_hook( __FILE__,array($custom_mod_rewrite,'flush_rewrite_rules'));
register_deactivation_hook( __FILE__,'flush_rewrite_rules'));
add_action('generate_rewrite_rules',"generate_rewrite_rules"));

class custom_mod_rewrite {
    function __construct() {
        $this->wp_rewrite = & $GLOBALS["wp_rewrite"];
    }
    function flush_rewrite_rules() {
        $this->wp_rewrite->flush_rules();
    }
    function generate_rewrite_rules() {

        $non_wp_rules = array(
            'simple-redirect/?$plugin_name' => 'http://google.com',$rules);
    }
}

此外,停用插件时,它不会更改回先前的规则.我只是遵循了codex示例,只是将其设置为在停用插件时刷新规则.所以应该有一些代码来删除添加的规则.

作为旁注,根据the codex,

Flushing the rewrite rules is an expensive operation,… … you should flush rewrite rules on the activation hook of a plugin,or when you know that the rewrite rules need to be changed

剩余问题:

>如果将其设置为默认永久链接,则不会添加规则.
>停用插件时,它不会更改回先前的规则.

添加一些更改后,您必须“刷新”重写规则: http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读