WP_Rewrite::add_permastruct() WordPress Method
The WP_Rewrite::add_permastruct() method allows you to add a new permastruct for your site. A permastruct is a set of rules that determine how your permalinks are structured. By default, WordPress uses a permastruct called "pretty permalinks" which uses the %postname% tag to structure your permalinks. However, you can use this method to create a custom permastruct for your site. For example, let's say you want to structure your permalinks like this: http://example.com/%year%/%monthnum%/%postname% You would use the WP_Rewrite::add_permastruct() method like this: $ permastruct = array( '%year%' => '', '%monthnum%' => '', '%postname%' => '' ); $wp_rewrite->add_permastruct('custom_permastruct', $permastruct); This would tell WordPress to use your custom permastruct when generating permalinks for your site.
WP_Rewrite::add_permastruct( string $name, string $struct, array $args = array() ) #
Adds a new permalink structure.
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.
Parameters
- $name
(string)(Required)Name for permalink structure.
- $struct
(string)(Required)Permalink structure (e.g. category/%category%)
- $args
(array)(Optional)Arguments for building rewrite rules based on the permalink structure.
- 'with_front'
(bool) Whether the structure should be prepended withWP_Rewrite::$front. Default true. - 'ep_mask'
(int) The endpoint mask defining which endpoints are added to the structure. Accepts a mask of:EP_ALLEP_NONEEP_ALL_ARCHIVESEP_ATTACHMENTEP_AUTHORSEP_CATEGORIESEP_COMMENTSEP_DATEEP_DAYEP_MONTHEP_PAGESEP_PERMALINKEP_ROOTEP_SEARCHEP_TAGSEP_YEARDefaultEP_NONE.
- 'paged'
(bool) Whether archive pagination rules should be added for the structure. Default true. - 'feed'
(bool) Whether feed rewrite rules should be added for the structure. Default true. - 'forcomments'
(bool) Whether the feed rules should be a query for a comments feed. Default false. - 'walk_dirs'
(bool) Whether the 'directories' making up the structure should be walked over and rewrite rules built for each in-turn. Default true. - 'endpoints'
(bool) Whether endpoints should be applied to the generated rules. Default true.
Default value: array()
- 'with_front'
Source
File: wp-includes/class-wp-rewrite.php
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;
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |