0

I am trying to get a custom query string 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 Mar 19 '18 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 Mar 19 '18 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 Mar 19 '18 at 21:25
  • @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 Apr 15 '18 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 Apr 15 '18 at 15:10
0

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' );

get_query_var('token');
get_query_var('username');

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;
        $wpdb->insert( 
            'wp_token',
            array( 
                '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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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