WP_Rewrite::add_rewrite_tag()publicWP 1.5.0

Adds or updates existing rewrite tags (e.g. %postname%).

If the tag already exists, replace the existing pattern and query for that tag, otherwise add the new tag.

{} It's a method of the class: WP_Rewrite{}

No Hooks.


null. Nothing.


global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
$tag(string) (required)
Name of the rewrite tag to add or update.
$regex(string) (required)
Regular expression to substitute the tag for in rewrite rules.
$query(string) (required)
String to append to the rewritten query. Must end in '='.


  • See: WP_Rewrite::$rewritecode
  • See: WP_Rewrite::$rewritereplace
  • See: WP_Rewrite::$queryreplace


Since 1.5.0 Introduced.

Code of WP_Rewrite::add_rewrite_tag() WP 5.9.3

public function add_rewrite_tag( $tag, $regex, $query ) {
	$position = array_search( $tag, $this->rewritecode, true );
	if ( false !== $position && null !== $position ) {
		$this->rewritereplace[ $position ] = $regex;
		$this->queryreplace[ $position ]   = $query;
	} else {
		$this->rewritecode[]    = $tag;
		$this->rewritereplace[] = $regex;
		$this->queryreplace[]   = $query;