WP_Rewrite::add_rewrite_tag() WordPress Method

WP_Rewrite::add_rewrite_tag() allows you to add a new rewrite tag and map it to a regular expression. This is useful for custom permalinks or for creating special pages.

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.

Top ↑

See also

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

Top ↑

Parameters

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


Top ↑

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)


Top ↑

Source

File: wp-includes/class-wp-rewrite.php

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


Top ↑

Changelog

Changelog
VersionDescription
1.5.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.