Skip Navigation

[Resolved] Add default parameters to all urls

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Shane 3 years ago.

Assisted by: Shane.

Author
Posts
#2240623

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

#2240659

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

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

#2240757

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;
}
#2240909

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

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

#2241299

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

#2241325

Hi
The page_link code broke my menu links.
I use Impreza on this site.

Truls

#2241649

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

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