Make WordPress Core

Changeset 27304


Ignore:
Timestamp:
02/26/2014 11:57:10 PM (11 years ago)
Author:
nacin
Message:

Add a $default argument to get_query_var() and WP_Query::get(). fixes #16471.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r27211 r27304  
    1919 * @uses $wp_query
    2020 *
    21  * @param string $var The variable key to retrieve.
     21 * @param string $var       The variable key to retrieve.
     22 * @param mixed  $default   Value to return if the query variable is not set. Default ''.
    2223 * @return mixed
    2324 */
    24 function get_query_var($var) {
     25function get_query_var( $var, $default = '' ) {
    2526    global $wp_query;
    2627
    27     return $wp_query->get($var);
     28    return $wp_query->get( $var, $default );
    2829}
    2930
     
    21312132     *
    21322133     * @param string $query_var Query variable key.
     2134     * @param mixed  $default   Value to return if the query variable is not set. Default ''.
    21332135     * @return mixed
    21342136     */
    2135     function get($query_var) {
    2136         if ( isset($this->query_vars[$query_var]) )
    2137             return $this->query_vars[$query_var];
    2138 
    2139         return '';
     2137    function get( $query_var, $default = '' ) {
     2138        if ( isset( $this->query_vars[ $query_var ] ) ) {
     2139            return $this->query_vars[ $query_var ];
     2140        }
     2141
     2142        return $default;
    21402143    }
    21412144
  • trunk/tests/phpunit/tests/query.php

    r27067 r27304  
    7878        }
    7979    }
     80
     81    /**
     82     * @ticket 16471
     83     */
     84    function test_default_query_var() {
     85        $query = new WP_Query;
     86        $this->assertEquals( '', $query->get( 'nonexistent' ) );
     87        $this->assertFalse( $query->get( 'nonexistent', false ) );
     88        $this->assertTrue( $query->get( 'nonexistent', true ) );
     89    }
    8090}
Note: See TracChangeset for help on using the changeset viewer.