Skip to content

Instantly share code, notes, and snippets.

@fikrirasyid
Created September 12, 2014 06:01
Show Gist options
  • Save fikrirasyid/695c155cdd4ee78760fc to your computer and use it in GitHub Desktop.
Save fikrirasyid/695c155cdd4ee78760fc to your computer and use it in GitHub Desktop.
WordPress - Modifying URL's Query String, The Easy Way
/**
* Modify URL's query string
*
* @param string url to be modified
* @param array of query strings
*
* @return string of modified url
*/
function fr_modify_url( $url = '', $query_string_args = array() ){
// Parse url to get query strings
$parsed_url = parse_url( $url );
if( isset( $parsed_url['query'] ) ){
parse_str( $parsed_url['query'], $query_strings_default );
} else {
$query_strings_default = array();
}
// Parse the default query strings against the given one
$query_strings = wp_parse_args( $query_string_args, $query_strings_default );
$url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
if( isset( $parsed_url['path'] ) ){
$url .= $parsed_url['path'];
}
$url .= '?' . build_query( $query_strings );
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment