apply_filters( ‘rewrite_rules_array’, string[] $rules )

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

VersionDescription
1.5.0Introduced.

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’.


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

You must log in before being able to contribute a note or feedback.