WordPress at a glance

add_rewrite_tag() WP 1.0

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

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.

  • Global. WP_Rewrite. $wp_rewrite
  • Global. WP. $wp
Works based on: WP_Rewrite()

No Hooks.

Return

Nothing (null).

Usage

add_rewrite_tag( $tag, $regex, $query );
$tag(string) (required)
Name of the new rewrite tag.
$regex(string) (required)
Regular expression to substitute the tag for in rewrite rules.
$query(string)
String to append to the rewritten query. Must end in '='.
Default: ''

Code of add_rewrite_tag: wp-includes/rewrite.php VER 4.9.8

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

Related Functions

From tag: Rewrite (clean friendly URL)

No comments
    Hello, !     Log In . Register