A quickie post about a few functions that I’ve only recently discovered, and really wish I’d discovered sooner! One is a PHP function, two are WordPress core functions. Both help immensely when you’re dealing with URLs.
parse_url is a PHP function that takes a full URL and returns an array populated with all the components, e.g. the host, the path, the query string, etc. For dealing with the query string you might also want to look at
http_build_query, which I think the following two WP functions make use of.
add_query_arg is incredibly useful if you want to add something to a URL’s query string without affecting the query parameters already in place. It defaults to the current URL, and you can either just add a key / value pair, or give it an array of key / value pairs to add.
remove_query_arg is simply the reverse of
All together, manipulating URLs becomes a much simpler process with these handy functions!