add_query_arg

Home » Functions » add_query_arg

Function Name: add_query_arg

If you’ve worked with WordPress for a while, you’ve likely come across the need to add parameters to the URL of a specific page or post. The add_query_arg() function is the perfect tool for this task.

This function enables you to add parameters to a URL query string gracefully. It takes two parameters: the first is the name of the parameter, and the second is the value for that parameter.

The function then adds this parameter and its value to the URL’s query string and returns the new URL as a string. If there were already parameters in the URL, the new parameter is appended to the end of the query string.

Here’s an example of a typical usage of the add_query_arg() function:

$current_url = add_query_arg( 'action', 'delete', 'http://example.com/some-page/' );

In this example, the function adds the "action" parameter with a value of "delete" to the URL http://example.com/some-page/. It then returns the new URL with the added parameter as a string.

Overall, the add_query_arg() function is incredibly useful for adding parameters to a URL query string in WordPress development.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.