add_query_arg

Retrieves a modified URL query string.

Description

(string) add_query_arg(); 

You can rebuild the URL and append a new query variable to the URL query by using this function. You can also retrieve the full URL with query data.

Adding a single key & value or an associative array. Setting a key value to emptystring removes the key. Omitting oldquery_or_uri uses the $_SERVER value.

Returns (string)

New URL query string.


Usage

  1. if ( !function_exists( 'add_query_arg' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/backpress/functions.core.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = add_query_arg(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/functions.core.php  
  1. function add_query_arg() { 
  2. $ret = ''; 
  3. if ( is_array( func_get_arg(0) ) ) { 
  4. if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) ) 
  5. $uri = $_SERVER['REQUEST_URI']; 
  6. else 
  7. $uri = @func_get_arg( 1 ); 
  8. } else { 
  9. if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) ) 
  10. $uri = $_SERVER['REQUEST_URI']; 
  11. else 
  12. $uri = @func_get_arg( 2 ); 
  13.  
  14. if ( $frag = strstr( $uri, '#' ) ) 
  15. $uri = substr( $uri, 0, -strlen( $frag ) ); 
  16. else 
  17. $frag = ''; 
  18.  
  19. if ( preg_match( '|^https?://|i', $uri, $matches ) ) { 
  20. $protocol = $matches[0]; 
  21. $uri = substr( $uri, strlen( $protocol ) ); 
  22. } else { 
  23. $protocol = ''; 
  24.  
  25. if ( strpos( $uri, '?' ) !== false ) { 
  26. $parts = explode( '?', $uri, 2 ); 
  27. if ( 1 == count( $parts ) ) { 
  28. $base = '?'; 
  29. $query = $parts[0]; 
  30. } else { 
  31. $base = $parts[0] . '?'; 
  32. $query = $parts[1]; 
  33. } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) { 
  34. $base = $uri . '?'; 
  35. $query = ''; 
  36. } else { 
  37. $base = ''; 
  38. $query = $uri; 
  39.  
  40. wp_parse_str( $query, $qs ); 
  41. $qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string 
  42. if ( is_array( func_get_arg( 0 ) ) ) { 
  43. $kayvees = func_get_arg( 0 ); 
  44. $qs = array_merge( $qs, $kayvees ); 
  45. } else { 
  46. $qs[func_get_arg( 0 )] = func_get_arg( 1 ); 
  47.  
  48. foreach ( (array) $qs as $k => $v ) { 
  49. if ( $v === false ) 
  50. unset( $qs[$k] ); 
  51.  
  52. $ret = build_query( $qs ); 
  53. $ret = trim( $ret, '?' ); 
  54. $ret = preg_replace( '#=(&|$)#', '$1', $ret ); 
  55. $ret = $protocol . $base . $ret . $frag; 
  56. $ret = rtrim( $ret, '?' ); 
  57. return $ret;