add_rewrite_tag
Add a new rewrite tag (like %postname%).
Description
The $query
parameter is optional. If it is omitted you must ensure that you call this on, or before, the hook. This is because $query
defaults to $tag
=,, and for this to work a new query var has to be added.
Parameters (3)
- 0. $tag (string)
- Name of the new rewrite tag.
- 1. $regex (string)
- Regular expression to substitute the tag for in rewrite rules.
- 2. $query — Optional. (string) =>
''
- String to append to the rewritten query. Must end in =.. Default empty.
Usage
if ( !function_exists( 'add_rewrite_tag' ) ) { require_once ABSPATH . WPINC . '/rewrite.php'; } // Name of the new rewrite tag. $tag = ''; // Regular expression to substitute the tag for in rewrite rules. $regex = ''; // Optional. String to append to the rewritten query. Must end in '='. Default empty. $query = ''; // NOTICE! Understand what this does before running. $result = add_rewrite_tag($tag, $regex, $query);
Defined (1)
The function is defined in the following location(s).
- /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 );
- }