| 1 | Index: wp-includes/rewrite.php |
|---|
| 2 | =================================================================== |
|---|
| 3 | --- wp-includes/rewrite.php (revision 19735) |
|---|
| 4 | +++ wp-includes/rewrite.php (working copy) |
|---|
| 5 | @@ -22,26 +22,33 @@ |
|---|
| 6 | } |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | - * Add a new tag (like %postname%). |
|---|
| 10 | + * Add a new rewrite tag (like %postname%). |
|---|
| 11 | * |
|---|
| 12 | - * Warning: you must call this on init or earlier, otherwise the query var |
|---|
| 13 | - * addition stuff won't work. |
|---|
| 14 | + * The $query parameter is optional. If it is omitted you must ensure that |
|---|
| 15 | + * you call this on, or before, the 'init' hook. This is because $query defaults |
|---|
| 16 | + * to "$tag=", and for this to work a new query var has to be added. |
|---|
| 17 | * |
|---|
| 18 | + * @see WP_Rewrite::add_rewrite_tag() |
|---|
| 19 | * @since 2.1.0 |
|---|
| 20 | * |
|---|
| 21 | - * @param string $tagname |
|---|
| 22 | - * @param string $regex |
|---|
| 23 | + * @param string $tag Name of the new rewrite tag. |
|---|
| 24 | + * @param string $regex Regular expression to substitute the tag for in rewrite rules. |
|---|
| 25 | + * @param string $query String to append to the rewritten query. Must end in '='. Optional. |
|---|
| 26 | */ |
|---|
| 27 | -function add_rewrite_tag($tagname, $regex) { |
|---|
| 28 | - //validation |
|---|
| 29 | - if ( strlen($tagname) < 3 || $tagname[0] != '%' || $tagname[strlen($tagname)-1] != '%' ) |
|---|
| 30 | +function add_rewrite_tag( $tag, $regex, $query = '' ) { |
|---|
| 31 | + // validate the tag's name |
|---|
| 32 | + if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' ) |
|---|
| 33 | return; |
|---|
| 34 | |
|---|
| 35 | - $qv = trim($tagname, '%'); |
|---|
| 36 | + global $wp_rewrite, $wp; |
|---|
| 37 | |
|---|
| 38 | - global $wp_rewrite, $wp; |
|---|
| 39 | - $wp->add_query_var($qv); |
|---|
| 40 | - $wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '='); |
|---|
| 41 | + if ( empty( $query ) ) { |
|---|
| 42 | + $qv = trim( $tag, '%' ); |
|---|
| 43 | + $wp->add_query_var( $qv ); |
|---|
| 44 | + $query = $qv . '='; |
|---|
| 45 | + } |
|---|
| 46 | + |
|---|
| 47 | + $wp_rewrite->add_rewrite_tag( $tag, $regex, $query ); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | @@ -1137,30 +1141,27 @@ |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | - * Append or update tag, pattern, and query for replacement. |
|---|
| 56 | + * Add or update existing rewrite tags (e.g. %postname%). |
|---|
| 57 | * |
|---|
| 58 | * If the tag already exists, replace the existing pattern and query for |
|---|
| 59 | * that tag, otherwise add the new tag, pattern, and query to the end of the |
|---|
| 60 | * arrays. |
|---|
| 61 | * |
|---|
| 62 | - * @internal What is the purpose of this function again? Need to finish long |
|---|
| 63 | - * description. |
|---|
| 64 | - * |
|---|
| 65 | * @since 1.5.0 |
|---|
| 66 | * @access public |
|---|
| 67 | * |
|---|
| 68 | - * @param string $tag Append tag to rewritecode property array. |
|---|
| 69 | - * @param string $pattern Append pattern to rewritereplace property array. |
|---|
| 70 | - * @param string $query Append query to queryreplace property array. |
|---|
| 71 | + * @param string $tag Name of the rewrite tag to add or update. |
|---|
| 72 | + * @param string $regex Regular expression to substitute the tag for in rewrite rules. |
|---|
| 73 | + * @param string $query String to append to the rewritten query. Must end in '='. |
|---|
| 74 | */ |
|---|
| 75 | - function add_rewrite_tag($tag, $pattern, $query) { |
|---|
| 76 | - $position = array_search($tag, $this->rewritecode); |
|---|
| 77 | + function add_rewrite_tag( $tag, $regex, $query ) { |
|---|
| 78 | + $position = array_search( $tag, $this->rewritecode ); |
|---|
| 79 | if ( false !== $position && null !== $position ) { |
|---|
| 80 | - $this->rewritereplace[$position] = $pattern; |
|---|
| 81 | - $this->queryreplace[$position] = $query; |
|---|
| 82 | + $this->rewritereplace[ $position ] = $regex; |
|---|
| 83 | + $this->queryreplace[ $position ] = $query; |
|---|
| 84 | } else { |
|---|
| 85 | $this->rewritecode[] = $tag; |
|---|
| 86 | - $this->rewritereplace[] = $pattern; |
|---|
| 87 | + $this->rewritereplace[] = $regex; |
|---|
| 88 | $this->queryreplace[] = $query; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|