WordPress Code Snippets

A fresh mix of code snippets and tutorials

PHP Add and Remove Query String Variables


Two quick functions, one for adding a query string variable and another to remove a query string variable. Either of these functions can be used in any PHP script to modify query-string parameters.

Add Query String Variable

Here is a function that will add the specified query-string variable (key and value) to the specified URL:

This function accepts three variables:

  • $url – URL to which the query-string variable should be added
  • $key – query-string variable key
  • $value – query-string variable value

So for example, if you have the following URL:


..and you would like to add a query-string variable such as goodbye=nightclub, we can use the previous function like so:

The resulting URL will look like this:


Note that if the specified $key exists in the $url, it will be replaced.

Remove Query String Variable

Here is a similar function that removes the specified variable from the specified URL:

This function accepts only two variables, one for the URL and another for the variable key. For example, let’s say we want to remove hello=world from the previous URL. We can do this:

..and the result will look like this:


Source: addedbytes.com

Show Support

Like our new Facebook Page to show support!