Steve Taylor photo

Manipulating URLs in PHP and WordPress

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 parse_string.

There’s also 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 add_query_arg.

All together, manipulating URLs becomes a much simpler process with these handy functions!

One comment

  1. Jem avatar Jem

    Discovered this by accident while looking for a cheaty way to manipulate a complicated query string with PHP (and just happen to need it FOR WordPress).

    It’s just a shame that the two WP functions don’t seem to support adding multiple values to a key (not without me doing some PHP legwork myself, anyway).

    Thanks for the tips anyway – every day I discover new capabilities of WP.

Comments are closed.