Adding GET params to a URL in WordPress with add_query_arg

I am finding myself using this function a lot now, and I constantly forget the function name:

add_query_arg();

It allows you to pass in some additional parameters, and a URL, and receive back the URL with the query string params added. For example:

$some_url = "http://simonwheatley.co.uk/?stuff=whatever";
$params = array( 'wp_siteurl' => "http://www.example.com" );
$some_url = add_query_arg( $params, $some_url );

This will result in a url something like this:

http://simonwheatley.co.uk/?stuff=whatever&wp_siteurl=http%3A%2F%2Fwww.example.com

Pretty cool, huh? So now you don’t need to worry about what parameters are already in the URL, you can just hand all that hassle to add_query_arg.

Update: See also remove_query_arg.

5 thoughts on “Adding GET params to a URL in WordPress with add_query_arg

  1. Lucky Shot

    Thanks Simon!
    I am tweaking WordPress to be like a CMS and I am looking for good ways to combine WP with raw PHP in it and make it powerful and sending parameters is a great deal.
    You explain how to send the params, but is there a better way to read them rather than just using $_GET[‘stuff’] integrated in the core WordPress? (I mean without using plugins)

    Cheers

    Reply
  2. Marc

    Cheers Simon, I too, like Lucky Shot am trying to make it look more like a regular website and less like a blog… I had achieved this in the past using Smarty but the solution wasn’t as portable and you couldn’t just switch to another theme easily at all…
    I’d love to compare notes with Lucky Shot too!

    Reply
  3. Peter

    Hallelujah. I’m eternally forgetting, and then searching for for this function, and I never seem to be able to find it. Thanks for posting on it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>