WP_Rewrite::add_permastruct( string $name, string $struct, array $args = array() )
- Since
- 2.5.0
- Source
wp-includes/class-wp-rewrite.php:1818
Description
A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; it is an easy way of expressing a set of regular expressions that rewrite to a set of query strings. The new permastruct is added to the WP_Rewrite::$extra_permastructs array.
When the rewrite rules are built by WP_Rewrite::rewrite_rules(), all of these extra permastructs are passed to WP_Rewrite::generate_rewrite_rules() which transforms them into the regular expressions that many love to hate.
The $args parameter gives you control over how WP_Rewrite::generate_rewrite_rules() works on the new permastruct.
Compatibility
- WordPress
- since 2.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
$namestring- Name for permalink structure.
$structstring- Permalink structure (e.g. category/%category%)
$argsarrayoptional- Arguments for building rewrite rules based on the permalink structure. Default empty array.Default:
array()$with_frontbooldefault: trueWhether the structure should be prepended withWP_Rewrite::$front.$ep_maskintdefault: EP_NONEThe endpoint mask defining which 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_YEAR$pagedbooldefault: trueWhether archive pagination rules should be added for the structure.$feedbooldefault: trueWhether feed rewrite rules should be added for the structure.$forcommentsbooldefault: falseWhether the feed rules should be a query for a comments feed.$walk_dirsbooldefault: trueWhether the 'directories' making up the structure should be walked over and rewrite rules built for each in-turn.$endpointsbooldefault: trueWhether endpoints should be applied to the generated rules.
Performance profile
How much work a call to WP_Rewrite::add_permastruct() 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
- Trivial
- Scaling
- Constant
- Instructions
- 38–46
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 49.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Rewrite::add_permastruct() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 38–41 | array_intersect_key(), wp_parse_args() |
| always | 43–46 | func_get_arg(), array_intersect_key(), wp_parse_args() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 49 instructions, 38–46 executed per call, 3 branches. The work does not change between versions.
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 · 1
- wp_parse_args()Merges user defined arguments into defaults array.
Source code
public function add_permastruct( $name, $struct, $args = array() ) { // Back-compat for the old parameters: $with_front and $ep_mask. if ( ! is_array( $args ) ) { $args = array( 'with_front' => $args ); } if ( func_num_args() === 4 ) { $args['ep_mask'] = func_get_arg( 3 ); } $defaults = array( 'with_front' => true, 'ep_mask' => EP_NONE, 'paged' => true, 'feed' => true, 'forcomments' => false, 'walk_dirs' => true, 'endpoints' => true, ); $args = array_intersect_key( $args, $defaults ); $args = wp_parse_args( $args, $defaults ); if ( $args['with_front'] ) { $struct = $this->front . $struct; } else { $struct = $this->root . $struct; } $args['struct'] = $struct; $this->extra_permastructs[ $name ] = $args; }Changelog
Introduced in 2.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 6.9.7 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.