Hi.
I have a site where I would like to add two parameters to all URLs and only when a user is logged in. The parameter values are different from user to user.
Can you point me in the right direction?
Truls
Hi Truls,
Thank you for getting in touch.
This should be possible by using the function below.
add_action('wp_head', 'your_function');
function your_function(){
if ( is_user_logged_in() ) {
$genre_url = add_query_arg('aff', '1234567', get_permalink());
}
}
You can add this to your custom functions at Toolset -> Settings -> Custom Code. Ensure that you've activated it and set it to run on the frontend only.
Secondly replace 'aff' with the name of the url parameter and 1234567 with the value you want to add to the parameter.
Thanks,
Shane
Do I need to have a return? Or call another wp function?
I see the result when I echo it to the screen, but it will not show in the URL.
add_action('wp_head', 'ais_url_param');
function ais_url_param()
{
if (is_user_logged_in()) {
$vid = do_shortcode('[get_user_vid]');
$bid = do_shortcode('[wpv-user field="ID"]');
$genre_url = esc_url(add_query_arg(
array(
'vid' => $vid,
'bid' => $bid,),
get_permalink()
));
}
echo $genre_url;
}
Hello,
I did some more checks on this one and the correct function to use is the one below.
function wpd_append_query_string_page( $url, $id ) {
$url = add_query_arg( 'ngg_force_update', 1, $url );
return $url;
}
add_filter( 'page_link', 'wpd_append_query_string', 10, 2 );
function wpd_append_query_string_post( $url, $id ) {
$url = add_query_arg( 'ngg_force_update', 1, $url );
return $url;
}
add_filter( 'post_link', 'wpd_append_query_string_post', 10, 2 );
function wpd_append_query_string_cpt( $url, $id ) {
$url = add_query_arg( 'ngg_force_update', 1, $url );
return $url;
}
add_filter( 'post_type_link', 'wpd_append_query_string_cpt', 10, 2 );
This will modify the links to add the parameter to all your links. Notice there is one for the Custom post type, Default Post Type and Pages.
So you can then modify/remove the functions as needed.
Please let me know if this helps.
Thanks,
Shane
Thank you, I will try this.
Is it possible to combine this in a switch conditional?
And is it better to put the code in Toolset -> Settings -> Custom Code than in functions.php?
Truls
Hi
The page_link code broke my menu links.
I use Impreza on this site.
Truls
Hi Truls,
Yes it is possible to combine this with a switch function. However the switch must be within the respective functions for the hooks.
What would be the condition for using the switch ?
The page_link code broke my menu links.
Is it that your urls are now going to 404 ? Are the parameters being added to the page url ?
Please let me know.
Thanks,
Shane