Make WordPress Core


Ignore:
Timestamp:
10/20/2004 09:28:37 PM (21 years ago)
Author:
rboren
Message:

Introducing add_query_arg() and remove_query_arg(). Props: Owen Winkler.

File:
1 edited

Legend:

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

    r1820 r1823  
    22462246}
    22472247
     2248/*
     2249add_query_arg: Returns a modified querystring by adding
     2250a single key & value or an associative array.
     2251Setting a key value to emptystring removes the key.
     2252Omitting oldquery_or_uri uses the $_SERVER value.
     2253
     2254Parameters:
     2255add_query_arg(newkey, newvalue, oldquery_or_uri) or
     2256add_query_arg(associative_array, oldquery_or_uri)
     2257*/
     2258function add_query_arg() {
     2259    $ret = '';
     2260    if(is_array(func_get_arg(0))) {
     2261        $uri = @func_get_arg(1);
     2262    }
     2263    else {
     2264        $uri = @func_get_arg(2);
     2265    }
     2266    if ('' == $uri) {
     2267        $uri = $_SERVER['REQUEST_URI'];
     2268    }
     2269    if (strstr($uri, '?')) {
     2270        $parts = explode('?', $uri, 2);
     2271        if (1 == count($parts)) {
     2272            $base = '?';
     2273            $query = $parts[0];
     2274        }
     2275        else {
     2276            $base = $parts[0] . '?';
     2277            $query = $parts[1];
     2278        }
     2279    }
     2280    else {
     2281        $base = $uri . '?';
     2282        $query = '';
     2283    }
     2284    parse_str($query, $qs);
     2285    if (is_array(func_get_arg(0))) {
     2286        $kayvees = func_get_arg(0);
     2287        $qs = array_merge($qs, $kayvees);
     2288    }
     2289    else
     2290    {
     2291            $qs[func_get_arg(0)] = func_get_arg(1);
     2292    }
     2293
     2294    foreach($qs as $k => $v)
     2295    {
     2296            if($v != '')
     2297        {
     2298                    if($ret != '') $ret .= '&';
     2299                    $ret .= "$k=$v";
     2300        }
     2301    }
     2302    $ret = $base . $ret;   
     2303    return trim($ret, '?');
     2304}
     2305
     2306function remove_query_arg($key, $query) {
     2307    add_query_arg($key, '', $query);
     2308}
    22482309?>
Note: See TracChangeset for help on using the changeset viewer.