add_rewrite_rule() WordPress Function

The add_rewrite_rule() function allows you to specify a new rewrite rule for your Wordpress site. This function takes two parameters: the first is the regex pattern that will trigger the rule, and the second is the replacement string that will be used when the rule is matched.

add_rewrite_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.


Top ↑

Parameters

$regex

(string)(Required)Regular expression to match request against.

$query

(string|array)(Required)The corresponding query vars for this rewrite rule.

$after

(string)(Optional) Priority of the new rule. Accepts 'top' or 'bottom'.

Default value: 'bottom'


Top ↑

More Information

add_rewrite_rule() allows you to specify additional rewrite rules for WordPress. It is most commonly used in conjunction with add_rewrite_tag() (which allows WordPress to recognize custom post/get variables).


Top ↑

Source

File: wp-includes/rewrite.php

function add_rewrite_rule( $regex, $query, $after = 'bottom' ) {
	global $wp_rewrite;

	$wp_rewrite->add_rule( $regex, $query, $after );
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Array support was added to the $query parameter.
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.