add_rewrite_tag

The timeline below displays how wordpress function add_rewrite_tag has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Adds 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 {@see 'init'} hook. This is because `$query` defaults to
 * `$tag=`, and for this to work a new query var has to be added.
 *
 * @since 2.1.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 * @global WP         $wp         Current WordPress environment instance.
 *
 * @param string $tag   Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
 */
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);
}

WordPress Version: 5.6

/**
 * 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 {@see 'init'} hook. This is because `$query` defaults to
 * `$tag=`, and for this to work a new query var has to be added.
 *
 * @since 2.1.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 * @global WP         $wp         Current WordPress environment instance.
 *
 * @param string $tag   Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
 */
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);
}

WordPress Version: 5.4

/**
 * 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 {@see 'init'} hook. This is because $query defaults
 * to "$tag=", and for this to work a new query var has to be added.
 *
 * @since 2.1.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 * @global WP         $wp         Current WordPress environment instance.
 *
 * @param string $tag   Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
 */
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);
}

WordPress Version: 5.3

/**
 * 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 {@see 'init'} hook. This is because $query defaults
 * to "$tag=", and for this to work a new query var has to be added.
 *
 * @since 2.1.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 * @global WP         $wp         Current WordPress environment instance.
 *
 * @param string $tag   Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
 */
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);
}

WordPress Version: 4.6

/**
 * 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 {@see 'init'} hook. This is because $query defaults
 * to "$tag=", and for this to work a new query var has to be added.
 *
 * @since 2.1.0
 *
 * @global WP_Rewrite $wp_rewrite
 * @global WP         $wp
 *
 * @param string $tag   Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
 */
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);
}

WordPress Version: 4.4

/**
 * 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.
 *
 * @since 2.1.0
 *
 * @global WP_Rewrite $wp_rewrite
 * @global WP         $wp
 *
 * @param string $tag   Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
 */
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);
}

WordPress Version: 4.3

/**
 * 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.
 *
 * @since 2.1.0
 *
 * @global WP_Rewrite $wp_rewrite
 * @global WP         $wp
 *
 * @param string $tag   Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query String to append to the rewritten query. Must end in '='. Optional.
 */
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);
}

WordPress Version: 3.7

/**
 * 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.
 *
 * @see WP_Rewrite::add_rewrite_tag()
 * @since 2.1.0
 *
 * @param string $tag Name of the new rewrite tag.
 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 * @param string $query String to append to the rewritten query. Must end in '='. Optional.
 */
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);
}