WP_Rewrite::generate_rewrite_rules( string $permalink_structure, int $ep_mask = EP_NONE, bool $paged = true, bool $feed = true, bool $forcomments = false, bool $walk_dirs = true, bool $endpoints = true ): string[]
- Since
- 1.5.0
- Source
wp-includes/class-wp-rewrite.php:874
Description
The main WP_Rewrite function for building the rewrite rule list. The contents of the function is a mix of black magic and regular expressions, so best just ignore the contents and move to the parameters.
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.
Parameters
$permalink_structurestring- The permalink structure.
$ep_maskintoptional- Endpoint mask defining what endpoints are added to the structure.
Accepts a mask of:
-EP_ALL
-EP_NONE
-EP_ALL_ARCHIVES
-EP_ATTACHMENT
-EP_AUTHORS
-EP_CATEGORIES
-EP_COMMENTS
-EP_DATE
-EP_DAY
-EP_MONTH
-EP_PAGES
-EP_PERMALINK
-EP_ROOT
-EP_SEARCH
-EP_TAGS
-EP_YEARDefaultEP_NONE.Default:EP_NONE $pagedbooloptional- Whether archive pagination rules should be added for the structure.
Default true.Default:true $feedbooloptional- Whether feed rewrite rules should be added for the structure.
Default true.Default:true $forcommentsbooloptional- Whether the feed rules should be a query for a comments feed.
Default false.Default:false $walk_dirsbooloptional- Whether the 'directories' making up the structure should be walked over and rewrite rules built for each in-turn. Default true.Default:
true $endpointsbooloptional- Whether endpoints should be applied to the generated rewrite rules.
Default true.Default:true
Return value
string[]- Array of rewrite rules keyed by their regex pattern.
Performance profile
How much work a call to WP_Rewrite::generate_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
- Heavy
- Scaling
- Scales with input
- Instructions
- 510
- Plugin surface
- None
- Called by
- 3
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5. The body compiles to 510.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below WP_Rewrite::generate_rewrite_rules() - cacheobject cache
wp_cache_get()one call below WP_Rewrite::generate_rewrite_rules() - serializeserialisation
maybe_unserialize()one call below WP_Rewrite::generate_rewrite_rules()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 509 | 509 | 51 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 510 | 510 | 51 | |
| 8.4 | 510 | 510 | 51 | 61 fewer instructions than PHP 8.3 |
| 8.3 | 571 | 571 | 51 | |
| 8.2 | 571 | 571 | 51 | 1 more instruction than PHP 8.1 |
| 8.1 | 570 | 570 | 51 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 571 | 571 | 51 |
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.
Uses · 5
- get_option()Retrieves an option value based on an option name.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_post_types()Gets a list of all registered post type objects.
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
- WP_Rewrite::preg_index()Indexes for matches for usage in preg_*() functions.
Used by · 3
- WP_Rewrite::generate_rewrite_rule()Generates rewrite rules with permalink structure and walking directory only.
- WP_Rewrite::page_rewrite_rules()Retrieves all of the rewrite rules for pages.
- WP_Rewrite::rewrite_rules()Constructs rewrite matches and queries from permalink structure.
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.