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.

Join the Conversation

5 Comments

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

  2. 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!

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.