add_rewrite_tag

Advertisement

Summery Summery

Add a new rewrite tag (like %postname%).

Syntax Syntax

add_rewrite_tag( string $tag, string $regex, string $query = '' )

Description Description

The $query parameter is optional. If it is omitted you must ensure that you call this on, or before, the ‘init’ hook. This is because $query defaults to "$tag=", and for this to work a new query var has to be added.

Parameters Parameters

$tag

(Required) Name of the new rewrite tag.

$regex

(Required) Regular expression to substitute the tag for in rewrite rules.

$query

(Optional) String to append to the rewritten query. Must end in '='.

Default value: ''

Source Source

File: wp-includes/rewrite.php

function add_rewrite_tag( $tag, $regex, $query = '' ) {
	// Validate the tag's name.
	if ( strlen( $tag ) < 3 || '%' !== $tag[0] || '%' !== $tag[ strlen( $tag ) - 1 ] ) {
		return;
	}

	global $wp_rewrite, $wp;

	if ( empty( $query ) ) {
		$qv = trim( $tag, '%' );
		$wp->add_query_var( $qv );
		$query = $qv . '=';
	}

	$wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
}

Advertisement

Changelog Changelog

Changelog
Version Description
2.1.0 Introduced.

Advertisement

Leave a Reply