WP_Rewrite::add_rewrite_tag( string $tag, string $regex, string $query )

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

Description

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

See also

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

Parameters

$tagstringrequired
Name of the rewrite tag to add or update.
$regexstringrequired
Regular expression to substitute the tag for in rewrite rules.
$querystringrequired
String to append to the rewritten query. Must end in '='.

More Information

Add an element to the $rewritecode, $rewritereplace and $queryreplace arrays using each parameter respectively. If $tag already exists in $rewritecode, the existing value will be overwritten.

See also: add_rewrite_tag($tagname, $regex)

Source

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;
	}
}

Changelog

VersionDescription
1.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.