WP_Post_Type::add_rewrite_rules()
- Since
- 4.6.0
- Source
wp-includes/class-wp-post-type.php:711
Compatibility
- WordPress
- since 4.6.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.
Performance profile
How much work a call to WP_Post_Type::add_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
- Moderate
- Scaling
- Constant
- Instructions
- 9–138
- Plugin surface
- None
- Called by
- 0
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 163.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below WP_Post_Type::add_rewrite_rules() - cacheobject cache
wp_cache_get()one call below WP_Post_Type::add_rewrite_rules() - serializeserialisation
maybe_unserialize()one call below WP_Post_Type::add_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.
What one call costs · 36 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Post_Type::add_rewrite_rules() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9–10 | none |
!is_post_type_viewable() | 15 | is_post_type_viewable() |
!is_admin() && !get_option() | 16–17 | is_admin(), get_option() |
is_post_type_viewable() | 20 | is_post_type_viewable(), ->add_query_var() |
!is_post_type_viewable() && !is_admin() && !get_option() | 22 | is_post_type_viewable(), is_admin(), get_option() |
is_post_type_viewable() && !is_admin() && !get_option() | 27 | is_post_type_viewable(), ->add_query_var(), is_admin(), get_option() |
is_admin() | 48–51 | is_admin(), add_rewrite_tag(), add_permastruct() |
!is_admin() && get_option() | 52–55 | is_admin(), get_option(), add_rewrite_tag(), add_permastruct() |
!is_post_type_viewable() && is_admin() | 54–56 | is_post_type_viewable(), is_admin(), add_rewrite_tag(), add_permastruct() |
!is_post_type_viewable() && !is_admin() && get_option() | 58–60 | is_post_type_viewable(), is_admin(), get_option(), add_rewrite_tag(), add_permastruct() |
is_post_type_viewable() && is_admin() | 59–61 | is_post_type_viewable(), ->add_query_var(), is_admin(), add_rewrite_tag(), add_permastruct() |
is_post_type_viewable() && !is_admin() && get_option() | 63–65 | is_post_type_viewable(), ->add_query_var(), is_admin(), get_option(), add_rewrite_tag(), add_permastruct() |
24 further outcomes, up to 138 instructions
is_admin() | 73–82 | is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_permastruct() |
!is_admin() && get_option() | 77–86 | is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && is_admin() | 79–87 | is_post_type_viewable(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && !is_admin() && get_option() | 83–91 | is_post_type_viewable(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && is_admin() | 84–92 | is_post_type_viewable(), ->add_query_var(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_permastruct() |
is_admin() | 86–95 | is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && !is_admin() && get_option() | 88–96 | is_post_type_viewable(), ->add_query_var(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_permastruct() |
!is_admin() && get_option() | 90–99 | is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && is_admin() | 92–100 | is_post_type_viewable(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && !is_admin() && get_option() | 96–104 | is_post_type_viewable(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && is_admin() | 97–105 | is_post_type_viewable(), ->add_query_var(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && !is_admin() && get_option() | 101–109 | is_post_type_viewable(), ->add_query_var(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_admin() && $wp_rewrite | 104–111 | is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_admin() && get_option() && $wp_rewrite | 108–115 | is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && is_admin() && $wp_rewrite | 110–116 | is_post_type_viewable(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && !is_admin() && get_option() && $wp_rewrite | 114–120 | is_post_type_viewable(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && is_admin() && $wp_rewrite | 115–121 | is_post_type_viewable(), ->add_query_var(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_admin() && $wp_rewrite | 117–124 | is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && !is_admin() && get_option() && $wp_rewrite | 119–125 | is_post_type_viewable(), ->add_query_var(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_admin() && get_option() && $wp_rewrite | 121–128 | is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && is_admin() && $wp_rewrite | 123–129 | is_post_type_viewable(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
!is_post_type_viewable() && !is_admin() && get_option() && $wp_rewrite | 127–133 | is_post_type_viewable(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && is_admin() && $wp_rewrite | 128–134 | is_post_type_viewable(), ->add_query_var(), is_admin(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
is_post_type_viewable() && !is_admin() && get_option() && $wp_rewrite | 132–138 | is_post_type_viewable(), ->add_query_var(), is_admin(), get_option(), add_rewrite_tag(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_rewrite_rule(), add_permastruct() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 163 | 9–138 | 15 | |
| 8.5 | 163 | 9–138 | 15 | |
| 8.4 | 163 | 9–138 | 15 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 171 | 9–146 | 15 | |
| 8.2 | 171 | 9–146 | 15 | |
| 8.1 | 171 | 9–146 | 15 | 6 fewer instructions than PHP 7.4 |
| 7.4 | 177 | 9–148 | 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.
Uses · 6
- is_post_type_viewable()Determines whether a post type is considered "viewable".
- is_admin()Determines whether the current request is for an administrative interface page.
- get_option()Retrieves an option value based on an option name.
- add_rewrite_tag()Adds a new rewrite tag (like %postname%).
- add_rewrite_rule()Adds a rewrite rule that transforms a URL structure to a set of query vars.
- add_permastruct()Adds a permalink structure.
Source code
public function add_rewrite_rules() { global $wp_rewrite, $wp; if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) { $wp->add_query_var( $this->query_var ); } if ( false !== $this->rewrite && ( is_admin() || get_option( 'permalink_structure' ) ) ) { if ( $this->hierarchical ) { add_rewrite_tag( "%$this->name%", '(.+?)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&pagename=" ); } else { add_rewrite_tag( "%$this->name%", '([^/]+)', $this->query_var ? "{$this->query_var}=" : "post_type=$this->name&name=" ); } if ( $this->has_archive ) { $archive_slug = true === $this->has_archive ? $this->rewrite['slug'] : $this->has_archive; if ( $this->rewrite['with_front'] ) { $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; } else { $archive_slug = $wp_rewrite->root . $archive_slug; } add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$this->name", 'top' ); if ( $this->rewrite['feeds'] && $wp_rewrite->feeds ) { $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$this->name" . '&feed=$matches[1]', 'top' ); } if ( $this->rewrite['pages'] ) { add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$this->name" . '&paged=$matches[1]', 'top' ); } } $permastruct_args = $this->rewrite; $permastruct_args['feed'] = $permastruct_args['feeds']; add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $permastruct_args ); } }Changelog
Introduced in 4.6.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-post-type.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.