WordPress add query string to URL
To add a query string to a URL in WordPress, follow these steps:
- Open the WordPress theme's functions.php file.
- Use the
add_query_arg()
function to add a query string parameter to the URL. - Save the changes to the functions.php file.
- Use the
get_permalink()
function to retrieve the modified URL with the added query string.
Example code:
// Step 1: Open the functions.php file
// Step 2: Use add_query_arg() to add a query string parameter
function add_query_string_to_url($url) {
$new_url = add_query_arg('your_parameter', 'parameter_value', $url);
return $new_url;
}
// Step 3: Save the changes to functions.php
// Step 4: Use get_permalink() to retrieve the modified URL
$original_url = get_permalink();
$modified_url = add_query_string_to_url($original_url);
Replace 'your_parameter' with the desired parameter name and 'parameter_value' with the desired value.