WP_Rewrite::rewrite_rules(): string[]
- Since
- 1.5.0
- Source
wp-includes/class-wp-rewrite.php:1276
Description
Runs the action 'generate_rewrite_rules' with the parameter that is an reference to the current WP_Rewrite instance to further manipulate the permalink structures and rewrite rules. Runs the 'rewrite_rules_array' filter on the full rewrite rule array.
There are two ways to manipulate the rewrite rules, one by hooking into the 'generate_rewrite_rules' action and gaining full control of the object or just manipulating the rewrite rule array before it is passed from the function.
Compatibility
- WordPress
- since 1.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
string[]- An associative array of matches and queries.
Performance profile
How much work a call to WP_Rewrite::rewrite_rules() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 3–199
- Plugin surface
- 11 hooks
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 292.
Third-party callbacks on 'post_rewrite_rules', 'date_rewrite_rules', 'root_rewrite_rules' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Rewrite::rewrite_rules() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($value) | 3 | none |
!empty($value) && !is_multisite() | 177–188 | home_url(), parse_url(), is_multisite(), ->generate_rewrite_rules(), apply_filters(), ->get_date_permastruct(), ->generate_rewrite_rules(), apply_filters(), ->generate_rewrite_rules(), apply_filters(), ->generate_rewrite_rules(), apply_filters(), ->get_search_permastruct(), ->generate_rewrite_rules(), apply_filters(), ->get_author_permastruct(), ->generate_rewrite_rules(), apply_filters(), ->page_rewrite_rules(), apply_filters(), array_merge(), apply_filters() |
!empty($value) && is_multisite() | 180–199 | home_url(), parse_url(), is_multisite(), is_main_site(), ->generate_rewrite_rules(), apply_filters(), ->get_date_permastruct(), ->generate_rewrite_rules(), apply_filters(), ->generate_rewrite_rules(), apply_filters(), ->generate_rewrite_rules(), apply_filters(), ->get_search_permastruct(), ->generate_rewrite_rules(), apply_filters(), ->get_author_permastruct(), ->generate_rewrite_rules(), apply_filters(), ->page_rewrite_rules(), apply_filters(), array_merge(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 292 | 3–199 | 15 | |
| 8.5 | 292 | 3–199 | 15 | |
| 8.4 | 292 | 3–199 | 15 | |
| 8.3 | 292 | 3–199 | 15 | |
| 8.2 | 292 | 3–199 | 15 | |
| 8.1 | 292 | 3–199 | 15 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 295 | 3–202 | 15 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 11
11 hooks fire while WP_Rewrite::rewrite_rules() runs, in this order:
- apply_filters( post_rewrite_rules )filterline 1319 (+43 into the body)
Filters rewrite rules used for "post" archives.
- apply_filters( date_rewrite_rules )filterline 1333 (+57 into the body)
Filters rewrite rules used for date archives.
- apply_filters( root_rewrite_rules )filterline 1348 (+72 into the body)
Filters rewrite rules used for root-level archives.
- apply_filters( comments_rewrite_rules )filterline 1362 (+86 into the body)
Filters rewrite rules used for comment feed archives.
- apply_filters( search_rewrite_rules )filterline 1378 (+102 into the body)
Filters rewrite rules used for search archives.
- apply_filters( author_rewrite_rules )filterline 1393 (+117 into the body)
Filters rewrite rules used for author archives.
- apply_filters( page_rewrite_rules )filterline 1405 (+129 into the body)
Filters rewrite rules used for "page" post type archives.
- apply_filters( {$permastructname}_rewrite_rules )filterline 1435 (+159 into the body)
Filters rewrite rules used for individual permastructs.
- do_action( tag_rewrite_rules )filter_deprecatedline 1447 (+171 into the body)
Filters rewrite rules used specifically for Tags.
- do_action( generate_rewrite_rules )actionline 1467 (+191 into the body)
Fires after the rewrite rules are generated.
- apply_filters( rewrite_rules_array )filterline 1476 (+200 into the body)
Filters the full set of generated rewrite rules.
Uses · 11
- home_url()Retrieves the URL for the current site where the front end is accessible.
- is_multisite()Determines whether Multisite is enabled.
- is_main_site()Determines whether a site is the main site of the current network.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- apply_filters_deprecated()Fires functions attached to a deprecated filter hook.
- do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
- WP_Rewrite::generate_rewrite_rules()Generates rewrite rules from a permalink structure.
- WP_Rewrite::get_date_permastruct()Retrieves date permalink structure, with year, month, and day.
- WP_Rewrite::get_search_permastruct()Retrieves the search permalink structure.
- WP_Rewrite::get_author_permastruct()Retrieves the author permalink structure.
- WP_Rewrite::page_rewrite_rules()Retrieves all of the rewrite rules for pages.
Used by · 2
- WP_Rewrite::mod_rewrite_rules()Retrieves mod_rewrite-formatted rewrite rules to write to .htaccess.
- WP_Rewrite::refresh_rewrite_rules()Refreshes the rewrite rules, saving the fresh value to the database.
Source code
public function rewrite_rules() { $rewrite = array(); if ( empty( $this->permalink_structure ) ) { return $rewrite; } // robots.txt -- only if installed at the root. $home_path = parse_url( home_url() ); $robots_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array(); // favicon.ico -- only if installed at the root. $favicon_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'favicon\.ico$' => $this->index . '?favicon=1' ) : array(); // sitemap.xml -- only if installed at the root. $sitemap_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'sitemap\.xml' => $this->index . '?sitemap=index' ) : array(); // Old feed and service files. $deprecated_files = array( '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old', '.*wp-app\.php(/.*)?$' => $this->index . '?error=403', ); // Registration rules. $registration_pages = array(); if ( is_multisite() && is_main_site() ) { $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true'; $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true'; } // Deprecated. $registration_pages['.*wp-register.php$'] = $this->index . '?register=true'; // Post rewrite rules. $post_rewrite = $this->generate_rewrite_rules( $this->permalink_structure, EP_PERMALINK ); /** * Filters rewrite rules used for "post" archives. * * @since 1.5.0 * * @param string[] $post_rewrite Array of rewrite rules for posts, keyed by their regex pattern. */ $post_rewrite = apply_filters( 'post_rewrite_rules', $post_rewrite ); // Date rewrite rules. $date_rewrite = $this->generate_rewrite_rules( $this->get_date_permastruct(), EP_DATE ); /** * Filters rewrite rules used for date archives. * * Likely date archives would include `/yyyy/`, `/yyyy/mm/`, and `/yyyy/mm/dd/`. * * @since 1.5.0 * * @param string[] $date_rewrite Array of rewrite rules for date archives, keyed by their regex pattern. */ $date_rewrite = apply_filters( 'date_rewrite_rules', $date_rewrite ); // Root-level rewrite rules. $root_rewrite = $this->generate_rewrite_rules( $this->root . '/', EP_ROOT ); /** * Filters rewrite rules used for root-level archives. * * Likely root-level archives would include pagination rules for the homepage * as well as site-wide post feeds (e.g. `/feed/`, and `/feed/atom/`). * * @since 1.5.0 * * @param string[] $root_rewrite Array of root-level rewrite rules, keyed by their regex pattern. */ $root_rewrite = apply_filters( 'root_rewrite_rules', $root_rewrite ); // Comments rewrite rules. $comments_rewrite = $this->generate_rewrite_rules( $this->root . $this->comments_base, EP_COMMENTS, false, true, true, false ); /** * Filters rewrite rules used for comment feed archives. *Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-rewrite.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.