钩子文档

rewrite_rules_array

💡 云策文档标注

概述

rewrite_rules_array 是一个 WordPress 过滤器钩子,用于过滤生成的完整重写规则数组。开发者可以通过此钩子修改或移除规则,但需注意规则保存到数据库后需手动刷新才能生效。

关键要点

  • 过滤器钩子:rewrite_rules_array,参数为 $rules(以正则模式为键的编译后规则数组)。
  • 用途:允许修改任何重写规则,例如添加、删除或调整规则。
  • 生效条件:重写规则保存到数据库,修改后需在“设置 > 固定链接”中刷新规则,或使用 flush_rules() 函数编程实现。
  • 重要提示:必须返回 $rules 数组,否则可能导致严重问题。
  • 相关函数:WP_Rewrite::rewrite_rules() 用于从固定链接结构构造重写匹配和查询。

代码示例

// 列出所有已注册的重写规则(调试用)
add_filter('rewrite_rules_array', function($rules){
    var_dump($rules);
    return $rules;
});

// 移除自定义文章类型 'foo' 的 feed URL 规则
add_filter('rewrite_rules_array', 'kill_feed_rewrites');
function kill_feed_rewrites($rules) {
    foreach ($rules as $rule => $rewrite) {
        if ( preg_match('/^foo.*(feed)/', $rule) ) {
            unset($rules[$rule]);
        }
    }
    return $rules;
}

注意事项

  • rewrite_rules_array 仅在 WP_Rewrite::rewrite_rules() 被调用时应用,因此 var_dump() 等调试操作只在规则生成时输出。
  • 直接访问 $wp_rewrite->rules 可能未填充,建议使用 $wp_rewrite->wp_rewrite_rules() 来确保获取完整规则。
  • 示例中展示了重写规则数组的结构,包括自定义文章类型 'foo' 的规则,帮助开发者理解数组格式。

📄 原文内容

Filters the full set of generated rewrite rules.

Parameters

$rulesstring[]
The compiled array of rewrite rules, keyed by their regex pattern.

More Information

  • This filter hook can be used to any rule in the rewrite rules array.
  • Since rewrite rules are saved to the database, you must flush/update your rules from your admin under Settings > Permalinks before your changes will take effect. You can also use the flush_rules() function to do this programmatically.
  • Make sure you return the $rules array or very bad things will happen.

Source

$this->rules = apply_filters( 'rewrite_rules_array', $this->rules );

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Here is a quick and dirty way you can list all Rewrite Rules registered on your site:

    add_filter('rewrite_rules_array', function($rules){
        var_dump($rules);
        return $rules;
    });

    You could also drop this somewhere on your page:

    global $wp_rewrite;  
    var_dump($wp_rewrite->rules);

  2. Skip to note 5 content

    Example migrated from Codex:

    Removing Feed URLs

    This example demonstrates how you would remove feed URLs for a custom post type called ‘foo’

    add_filter('rewrite_rules_array', 'kill_feed_rewrites');
    function kill_feed_rewrites($rules) {
    
        foreach ($rules as $rule => $rewrite) {
    
            if ( preg_match('/^foo.*(feed)/', $rule) ) {
                unset($rules[$rule]);
            }
    
        }
    
        return $rules;
    }

  3. Skip to note 6 content

    Example migrated from Codex:

    Sample Rewrite Rule Array

    This shows one example of what a var_dump() of a rewrite array might look like. Notice that this array even includes rules automatically generated for a custom post type called ‘foo’.

    <br />
    array (size=103)<br />
    'foo/?$' => string 'index.php?post_type=foo' (length=23)<br />
    'foo/page/([0-9]{1,})/?$' => string 'index.php?post_type=foo&paged;=$matches[1]' (length=41)<br />
    'wp-types-group/[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)<br />
    'wp-types-group/[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb;=1' (length=37)<br />
    'wp-types-group/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'wp-types-group/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'wp-types-group/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage;=$matches[2]' (length=50)<br />
    'wp-types-group/([^/]+)/trackback/?$' => string 'index.php?wp-types-group=$matches[1]&tb;=1' (length=41)<br />
    'wp-types-group/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?wp-types-group=$matches[1]&feed;=$matches[2]' (length=53)<br />
    'wp-types-group/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?wp-types-group=$matches[1]&feed;=$matches[2]' (length=53)<br />
    'wp-types-group/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?wp-types-group=$matches[1]&paged;=$matches[2]' (length=54)<br />
    'wp-types-group/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?wp-types-group=$matches[1]&cpage;=$matches[2]' (length=54)<br />
    'wp-types-group/([^/]+)(/[0-9]+)?/?$' => string 'index.php?wp-types-group=$matches[1]&page;=$matches[2]' (length=53)<br />
    'wp-types-group/[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)<br />
    'wp-types-group/[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb;=1' (length=37)<br />
    'wp-types-group/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'wp-types-group/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'wp-types-group/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage;=$matches[2]' (length=50)<br />
    'foo/[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)<br />
    'foo/[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb;=1' (length=37)<br />
    'foo/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'foo/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'foo/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage;=$matches[2]' (length=50)<br />
    'foo/([^/]+)/trackback/?$' => string 'index.php?foo=$matches[1]&tb;=1' (length=30)<br />
    'foo/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?foo=$matches[1]&feed;=$matches[2]' (length=42)<br />
    'foo/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?foo=$matches[1]&feed;=$matches[2]' (length=42)<br />
    'foo/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?foo=$matches[1]&paged;=$matches[2]' (length=43)<br />
    'foo/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?foo=$matches[1]&cpage;=$matches[2]' (length=43)<br />
    'foo/([^/]+)(/[0-9]+)?/?$' => string 'index.php?foo=$matches[1]&page;=$matches[2]' (length=42)<br />
    'foo/[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)<br />
    'foo/[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb;=1' (length=37)<br />
    'foo/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'foo/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    'foo/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage;=$matches[2]' (length=50)<br />
    'category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?category_name=$matches[1]&feed;=$matches[2]' (length=52)<br />
    'category/(.+?)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?category_name=$matches[1]&feed;=$matches[2]' (length=52)<br />
    'category/(.+?)/page/?([0-9]{1,})/?$' => string 'index.php?category_name=$matches[1]&paged;=$matches[2]' (length=53)<br />
    'category/(.+?)/?$' => string 'index.php?category_name=$matches[1]' (length=35)<br />
    'tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?tag=$matches[1]&feed;=$matches[2]' (length=42)<br />
    'tag/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?tag=$matches[1]&feed;=$matches[2]' (length=42)<br />
    'tag/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?tag=$matches[1]&paged;=$matches[2]' (length=43)<br />
    'tag/([^/]+)/?$' => string 'index.php?tag=$matches[1]' (length=25)<br />
    'type/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_format=$matches[1]&feed;=$matches[2]' (length=50)<br />
    'type/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?post_format=$matches[1]&feed;=$matches[2]' (length=50)<br />
    'type/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?post_format=$matches[1]&paged;=$matches[2]' (length=51)<br />
    'type/([^/]+)/?$' => string 'index.php?post_format=$matches[1]' (length=33)<br />
    'robots.txt$' => string 'index.php?robots=1' (length=18)<br />
    '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2).php$' => string 'index.php?feed=old' (length=18)<br />
    '.*wp-app.php(/.*)?$' => string 'index.php?error=403' (length=19)<br />
    '.*wp-register.php$' => string 'index.php?register=true' (length=23)<br />
    'feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed;=$matches[1]' (length=27)<br />
    '(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed;=$matches[1]' (length=27)<br />
    'page/?([0-9]{1,})/?$' => string 'index.php?&paged;=$matches[1]' (length=28)<br />
    'comments/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed;=$matches[1]&withcomments;=1' (length=42)<br />
    'comments/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?&feed;=$matches[1]&withcomments;=1' (length=42)<br />
    'comments/page/?([0-9]{1,})/?$' => string 'index.php?&paged;=$matches[1]' (length=28)<br />
    'search/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?s=$matches[1]&feed;=$matches[2]' (length=40)<br />
    'search/(.+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?s=$matches[1]&feed;=$matches[2]' (length=40)<br />
    'search/(.+)/page/?([0-9]{1,})/?$' => string 'index.php?s=$matches[1]&paged;=$matches[2]' (length=41)<br />
    'search/(.+)/?$' => string 'index.php?s=$matches[1]' (length=23)<br />
    'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?author_name=$matches[1]&feed;=$matches[2]' (length=50)<br />
    'author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?author_name=$matches[1]&feed;=$matches[2]' (length=50)<br />
    'author/([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?author_name=$matches[1]&paged;=$matches[2]' (length=51)<br />
    'author/([^/]+)/?$' => string 'index.php?author_name=$matches[1]' (length=33)<br />
    '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]&day;=$matches[3]&feed;=$matches[4]' (length=80)<br />
    '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]&day;=$matches[3]&feed;=$matches[4]' (length=80)<br />
    '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]&day;=$matches[3]&paged;=$matches[4]' (length=81)<br />
    '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]&day;=$matches[3]' (length=63)<br />
    '([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]&feed;=$matches[3]' (length=64)<br />
    '([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]&feed;=$matches[3]' (length=64)<br />
    '([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]&paged;=$matches[3]' (length=65)<br />
    '([0-9]{4})/([0-9]{1,2})/?$' => string 'index.php?year=$matches[1]&monthnum;=$matches[2]' (length=47)<br />
    '([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&feed;=$matches[2]' (length=43)<br />
    '([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?year=$matches[1]&feed;=$matches[2]' (length=43)<br />
    '([0-9]{4})/page/?([0-9]{1,})/?$' => string 'index.php?year=$matches[1]&paged;=$matches[2]' (length=44)<br />
    '([0-9]{4})/?$' => string 'index.php?year=$matches[1]' (length=26)<br />
    '.?.+?/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)<br />
    '.?.+?/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb;=1' (length=37)<br />
    '.?.+?/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    '.?.+?/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    '.?.+?/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage;=$matches[2]' (length=50)<br />
    '(.?.+?)/trackback/?$' => string 'index.php?pagename=$matches[1]&tb;=1' (length=35)<br />
    '(.?.+?)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?pagename=$matches[1]&feed;=$matches[2]' (length=47)<br />
    '(.?.+?)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?pagename=$matches[1]&feed;=$matches[2]' (length=47)<br />
    '(.?.+?)/page/?([0-9]{1,})/?$' => string 'index.php?pagename=$matches[1]&paged;=$matches[2]' (length=48)<br />
    '(.?.+?)/comment-page-([0-9]{1,})/?$' => string 'index.php?pagename=$matches[1]&cpage;=$matches[2]' (length=48)<br />
    '(.?.+?)(/[0-9]+)?/?$' => string 'index.php?pagename=$matches[1]&page;=$matches[2]' (length=47)<br />
    '[^/]+/attachment/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)<br />
    '[^/]+/attachment/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb;=1' (length=37)<br />
    '[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    '[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    '[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage;=$matches[2]' (length=50)<br />
    '([^/]+)/trackback/?$' => string 'index.php?name=$matches[1]&tb;=1' (length=31)<br />
    '([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?name=$matches[1]&feed;=$matches[2]' (length=43)<br />
    '([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?name=$matches[1]&feed;=$matches[2]' (length=43)<br />
    '([^/]+)/page/?([0-9]{1,})/?$' => string 'index.php?name=$matches[1]&paged;=$matches[2]' (length=44)<br />
    '([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?name=$matches[1]&cpage;=$matches[2]' (length=44)<br />
    '([^/]+)(/[0-9]+)?/?$' => string 'index.php?name=$matches[1]&page;=$matches[2]' (length=43)<br />
    '[^/]+/([^/]+)/?$' => string 'index.php?attachment=$matches[1]' (length=32)<br />
    '[^/]+/([^/]+)/trackback/?$' => string 'index.php?attachment=$matches[1]&tb;=1' (length=37)<br />
    '[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    '[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => string 'index.php?attachment=$matches[1]&feed;=$matches[2]' (length=49)<br />
    '[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => string 'index.php?attachment=$matches[1]&cpage;=$matches[2]' (length=50)<br />