I am trying to get a custom URL parameter from a URL in WordPress.

I add the following code to functions.php:

function add_query_vars_filter( $vars ){
    $vars[] = "token";
    return $vars;
add_filter( 'query_vars', 'add_query_vars_filter' );

Then on the webpage I write:

<?php  $token = get_query_var( 'token', $default = '');  ?>
<h1>Currently Browsing token <?php echo (int) $token; ?> On a 
static front page</h1>

and go to the webpage http://www.negovista.com/tokensuccess/?token=xxxxxx

However on the page is only printed

'Currently Browsing token On a static front page' without the xxxxxx

Anyone knows if there is an error in the code?

  • I don't see anything wrong in your code. Have you debugged the value of $token var?
    – cybmeta
    Commented Mar 19, 2018 at 9:10
  • 2
    In the source code of your page, both <?php $token = get_query_var( 'token', $default = ''); ?> and <?php echo (int) $token; ?> statements are printed as HTML comments (<!--?php $token = get_query_var( 'token', $default = ''); ?--> and <!--?php echo (int) $token; ?-->). It seems some error in the syntax wihtin your php template file.
    – cybmeta
    Commented Mar 19, 2018 at 9:22
  • Hi, thx for your answer! I think you are right. I am using a front-end editor called cornerstone and it is not possible to just put php-code inside it. I will have to write a shortcode and use that instead.
    – flemmingha
    Commented Mar 19, 2018 at 21:25
  • 1
    @cybmeta Could you explain your comment in an answer in a bit more detail? I'm having the exact same issue. Even copying and pasting the example from the WP Codex yields nothing. My code is function rj_add_query_vars_filter( $vars ){ $vars[] = "adminoption"; return $vars; } add_filter( 'query_vars', 'rj_add_query_vars_filter' ); echo get_query_var('adminoption', "<h1>NOT SET</h1>"); . My code always returns "NOT SET" my url query is "sometitle/?adminoption=TEST"
    – user658182
    Commented Apr 15, 2018 at 15:06
  • .. also using echo $_GET["adminoption"]; works fine. I can get that to print out whatever I want. This is in my functions.php file
    – user658182
    Commented Apr 15, 2018 at 15:10

1 Answer 1


I managed to find a solution with some help from some developers at the theme.co apex forum.

I needed to get two query strings from my URL www.randompage.com/tokensuccess/?token=token&username=username called token and username.

In order to get this I added the following code to the functions.php code:

function add_query_vars_filter( $vars ){
  $vars[] = "token";
  $vars[] = "username";
  return $vars;

add_filter( 'query_vars', 'add_query_vars_filter' );


This inserted the two query strings into the variables token and username

I then added the following code to the page.php file in order to print the values and insert them into the database.

<?php if (is_page('tokensuccess')) { if (get_query_var('token')) print "token = $token"; }  ?>

<?php if (is_page('tokensuccess')) { if (get_query_var('username')) print "username = $username"; }  ?>

<?php if (is_page('tokensuccess')) { 
    $token = get_query_var('token');
    $username = get_query_var('username');
    if( $token && $username ){

        global $wpdb;
                'token' => $token, 
                'username' => $username 


So now I can send a link with query strings to my customer and automatically get his/her token and name.

  • This does not seem to work on admin pages, I put the filter directly in the plugin root file. get_query_var returns an empty string. Commented Jan 27, 2021 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.