Created
September 12, 2014 06:01
-
-
Save fikrirasyid/695c155cdd4ee78760fc to your computer and use it in GitHub Desktop.
WordPress - Modifying URL's Query String, The Easy Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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