在 WordPress 插件中以编程方式添加 Mod 重写规则 [英] Adding Mod Rewrite Rules Programmatically in WordPress Plugin

查看:42
本文介绍了在 WordPress 插件中以编程方式添加 Mod 重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户更改永久链接设置时,以下示例插件将自定义 mod 重写规则添加到 .htaccess.

The following sample plugin adds the custom mod rewrite rules to .htaccess when the user changes the permalink settings.

/* 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);
    }
}

我发现了两个问题.

  1. 如果设置为默认永久链接,则不会添加规则.
  2. 更重要的是,除非用户更改永久链接设置,否则不会添加规则.(可以通过执行 $wp_rewrite->flush_rules() 解决在插件激活时)
  1. If it is set to the default permalink, it won't add the rules.
  2. More importantly, unless the user changes the permalink settings, the rules won't be added. (can be solved with $wp_rewrite->flush_rules() performed at the plugin activation)

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

For #2, I'm wondering if there is a good way to add the rules programmatically.

IIS(在 Windows 服务器上很常见)不支持 mod_rewrite.

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

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

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

It sounds like not all systems use .htaccess. So directly editing the .htaccess file may not be the best choice for a distributed plugin. I don't know. Probably I have to check if the server uses Apache and if so I need to check whether .htacess is writable and existing rules do not have the adding rules, then at last I can append the rules to it. Also when the user deactivate the plugin, the rules have to be erased. So it's kind of troublesome.

如果 WordPress 可以使用内置 API 或其他东西来处理它,我想把它留给 WordPress.但上面的例子是我目前所能找到的.所以我很感激你的信息.

If WordPress can handle it with a built-in API or something, I'd like to leave it to WordPress. But the above example was what I could have found so far. So I appreciate your information.

更新

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

As pfefferle suggested, I could use $wp_rewrite->flush_rules(). However, the problem #1 still persists; it won't take any effect when the default permalink settings is used. Any ideas?

/* 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__, array($custom_mod_rewrite, 'flush_rewrite_rules'));
add_action('generate_rewrite_rules', array($custom_mod_rewrite, "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',
            '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);
    }
}

此外,当停用插件时,它不会变回以前的规则.我只是按照 codex 示例进行操作,并在停用插件时将其设置为刷新规则.所以应该有一些代码来删除添加的规则.

Also, when deactivating the plugin, it doesn't change back to the previous rules. I just followed the codex example and just set it to flush the rules when deactivating the plugin. So there should be some code to delete the added rules.

作为旁注,根据法典

刷新重写规则是一个昂贵的操作,……你应该在插件的激活钩子上刷新重写规则,或者当你知道需要更改重写规则时

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

剩余问题:

  1. 如果设置为默认永久链接,则不会添加规则.
  2. 停用插件时,它不会变回以前的规则.

推荐答案

很遗憾,目前似乎没有有效的解决方案.

Unfortunately, there seems to be no effective solution for it at the moment.

这篇关于在 WordPress 插件中以编程方式添加 Mod 重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆