Make WordPress Core

Changeset 19756


Ignore:
Timestamp:
01/25/2012 10:48:24 PM (13 years ago)
Author:
duck_
Message:

Add the $query parameter to add_rewrite_tag() so that it matches WP_Rewrite::add_rewrite_tag(). Fixes #19871.

Also rename some other parameters so that they all match.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/rewrite.php

    r19753 r19756  
    2323
    2424/**
    25  * Add a new tag (like %postname%).
    26  *
    27  * Warning: you must call this on init or earlier, otherwise the query var
    28  * addition stuff won't work.
    29  *
     25 * Add a new rewrite tag (like %postname%).
     26 *
     27 * The $query parameter is optional. If it is omitted you must ensure that
     28 * you call this on, or before, the 'init' hook. This is because $query defaults
     29 * to "$tag=", and for this to work a new query var has to be added.
     30 *
     31 * @see WP_Rewrite::add_rewrite_tag()
    3032 * @since 2.1.0
    3133 *
    32  * @param string $tagname
    33  * @param string $regex
    34  */
    35 function add_rewrite_tag($tagname, $regex) {
    36     //validation
    37     if ( strlen($tagname) < 3 || $tagname[0] != '%' || $tagname[strlen($tagname)-1] != '%' )
     34 * @param string $tag Name of the new rewrite tag.
     35 * @param string $regex Regular expression to substitute the tag for in rewrite rules.
     36 * @param string $query String to append to the rewritten query. Must end in '='. Optional.
     37 */
     38function add_rewrite_tag( $tag, $regex, $query = '' ) {
     39    // validate the tag's name
     40    if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' )
    3841        return;
    3942
    40     $qv = trim($tagname, '%');
    41 
    4243    global $wp_rewrite, $wp;
    43     $wp->add_query_var($qv);
    44     $wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '=');
     44
     45    if ( empty( $query ) ) {
     46        $qv = trim( $tag, '%' );
     47        $wp->add_query_var( $qv );
     48        $query = $qv . '=';
     49    }
     50
     51    $wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
    4552}
    4653
     
    11701177
    11711178    /**
    1172      * Append or update tag, pattern, and query for replacement.
     1179     * Add or update existing rewrite tags (e.g. %postname%).
    11731180     *
    11741181     * If the tag already exists, replace the existing pattern and query for
    1175      * that tag, otherwise add the new tag, pattern, and query to the end of the
    1176      * arrays.
    1177      *
    1178      * @internal What is the purpose of this function again? Need to finish long
    1179      *           description.
    1180      *
    1181      * @since 1.5.0
    1182      * @access public
    1183      *
    1184      * @param string $tag Append tag to rewritecode property array.
    1185      * @param string $pattern Append pattern to rewritereplace property array.
    1186      * @param string $query Append query to queryreplace property array.
    1187      */
    1188     function add_rewrite_tag($tag, $pattern, $query) {
    1189         $position = array_search($tag, $this->rewritecode);
     1182     * that tag, otherwise add the new tag.
     1183     *
     1184     * @see WP_Rewrite::$rewritecode
     1185     * @see WP_Rewrite::$rewritereplace
     1186     * @see WP_Rewrite::$queryreplace
     1187     * @since 1.5.0
     1188     * @access public
     1189     *
     1190     * @param string $tag Name of the rewrite tag to add or update.
     1191     * @param string $regex Regular expression to substitute the tag for in rewrite rules.
     1192     * @param string $query String to append to the rewritten query. Must end in '='.
     1193     */
     1194    function add_rewrite_tag( $tag, $regex, $query ) {
     1195        $position = array_search( $tag, $this->rewritecode );
    11901196        if ( false !== $position && null !== $position ) {
    1191             $this->rewritereplace[$position] = $pattern;
    1192             $this->queryreplace[$position] = $query;
     1197            $this->rewritereplace[ $position ] = $regex;
     1198            $this->queryreplace[ $position ] = $query;
    11931199        } else {
    11941200            $this->rewritecode[] = $tag;
    1195             $this->rewritereplace[] = $pattern;
     1201            $this->rewritereplace[] = $regex;
    11961202            $this->queryreplace[] = $query;
    11971203        }
Note: See TracChangeset for help on using the changeset viewer.