WP_Rewrite::add_rule() public WP 2.1.0
Adds a rewrite rule that transforms a URL structure to a set of query vars.
Any value in the $after parameter that isn't 'bottom' will result in the rule being placed at the top of the rewrite rules.
{} It's a method of the class: WP_Rewrite{}
No Hooks.
Return
Null. Nothing.
Usage
global $wp_rewrite; $wp_rewrite->add_rule( $regex, $query, $after );
- $regex(string) (required)
- Regular expression to match request against.
- $query(string/array) (required)
- The corresponding query vars for this rewrite rule.
- $after(string)
- Priority of the new rule. Accepts 'top' or 'bottom'.
Default: 'bottom'
Changelog
Since 2.1.0 | Introduced. |
Since 4.4.0 | Array support was added to the $query parameter. |
Code of WP_Rewrite::add_rule() WP Rewrite::add rule WP 5.6
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 ) );
}
}
}