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.

Method of the class: WP_Rewrite{}

No Hooks.

Return

null. Nothing (null).

Usage

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 '='.

Notes

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

Changelog

Since 1.5.0 Introduced.

WP_Rewrite::add_rewrite_tag() code WP 6.5.2

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