On this page
WP_Rewrite::add_rule( string $regex, string|array $query, string $after = 'bottom' )
Adds a rewrite rule that transforms a URL structure to a set of query vars.
Description
Any value in the $after parameter that isn’t ‘bottom’ will result in the rule being placed at the top of the rewrite rules.
Parameters
$regexstring Required-
Regular expression to match request against.
$querystring|array Required-
The corresponding query vars for this rewrite rule.
$afterstring Optional-
Priority of the new rule. Accepts
'top'or'bottom'. Default'bottom'.Default:
'bottom'
Source
File: wp-includes/class-wp-rewrite.php. View all references
public function add_rule( $regex, $query, $after = 'bottom' ) {
if ( is_array( $query ) ) {
$external = false;
$query = add_query_arg( $query, 'index.php' );
} else {
$index = false === strpos( $query, '?' ) ? strlen( $query ) : strpos( $query, '?' );
$front = substr( $query, 0, $index );
$external = $front != $this->index;
}
// "external" = it doesn't correspond to index.php.
if ( $external ) {
$this->add_external_rule( $regex, $query );
} else {
if ( 'bottom' === $after ) {
$this->extra_rules = array_merge( $this->extra_rules, array( $regex => $query ) );
} else {
$this->extra_rules_top = array_merge( $this->extra_rules_top, array( $regex => $query ) );
}
}
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Rewrite::add_external_rule() wp-includes/class-wp-rewrite.php | Adds a rewrite rule that doesn’t correspond to index.php. |
| add_query_arg() wp-includes/functions.php | Retrieves a modified URL query string. |
Used By
| Used By | Description |
|---|---|
| add_rewrite_rule() wp-includes/rewrite.php | Adds a rewrite rule that transforms a URL structure to a set of query vars. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rewrite/add_rule