Append a parametter at first or last to a certain URL
-
26-04-2021 - |
Question
I want to change a URL and want to add a parameter to the last or first of that URL. For example I have URL like below
I want to append an extra parameter "&propertyType=RNT" the URL at last or first or in the middle whatever So the URL looks like below
OR
How can I achieve this ? The solution can be with PHP or using htaccess.
Thanks in advance
Solution
I got my code working now. Here is the below code that append that extra parameter to the end of the URL specified above.
add_action('init', 'add_custom_query_for_search');
function add_custom_query_for_search(){
global $wp;
$tempUrl = home_url() . add_query_arg( $wp->query_vars ) . '&propertyType=RNT';
if( ! isset($_GET['propertyType']) && (strpos($tempUrl, 'homes-for-sale-search') !== false) ){
wp_redirect($tempUrl);
die;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange