Passer la navigation

[Résolu] Using URL parameters

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

I want to display a URL parameter (e.g., ?usr=johndoe) inside a Toolset Field & Text block, but my shortcode using get_query_var() always returns "Guest" instead of the actual value.

Solution:

Use $_GET['usr'] instead of get_query_var() in your shortcode, and sanitize it like this:

function get_usr_param() {
    return isset($_GET['usr']) ? sanitize_text_field($_GET['usr']) : 'Guest';
}
add_shortcode('get_usr_param', 'get_usr_param');

Then use [get_usr_param] in the Field & Text block.

This support ticket is created Il y a 1 mois et 3 semaines. 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.

Ce sujet contient 1 réponse, a 1 voix.

Dernière mise à jour par Christopher Amirian Il y a 1 mois et 3 semaines.

Assisté par: Christopher Amirian.

Auteur
Publications
#2810315

Hello,
I am passing a URL parameter (user name) to a page like this:
mysite/mypage/?usr=johndoe
to be used in a view filter and it works fine. I would like to use the same URL parameter to be shown in a Field & Text block on the same page for something like this:
"This is the page of johndoe".
I wrote the following shortcode:

function get_usr_param() {
$qvars[] = 'usr';
$userpassed = get_query_var('usr', 'Guest');
return $userpassed;
}
add_shortcode('get_usr_param', 'get_usr_param');

the shortcode works but always returns "Guest".
Whatever other solution you might suggest to use aURL parameter in a Field & Text block is welcome.
Many thanks
Kind regards
Nicola

#2810412

Christopher Amirian
Supporter

Les langues: Anglais (English )

Hi Nicola,

This is not directly related to Toolset that is why it will be hard for us to give you a solution.

I think the issue lies in the use of get_query_var(). That function only works for registered query vars — typically those passed via rewrite rules, not standard $_GET parameters.

Since your URL looks like ?usr=johndoe, the value should be retrieved using $_GET['usr'] instead.

Something like this might work:

function get_usr_param() {
    return isset($_GET['usr']) ? sanitize_text_field($_GET['usr']) : 'Guest';
}
add_shortcode('get_usr_param', 'get_usr_param');


- This will output johndoe if the URL contains ?usr=johndoe.
- If the parameter is not present, it falls back to 'Guest'.

If you need additional help you can consider hiring a developer:

https://toolset.com/contractors/

Thanks.