admin管理员组

文章数量:1287838

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 /?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 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/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?

Share Improve this question edited Sep 9, 2021 at 18:42 Obase 32 bronze badges asked Mar 19, 2018 at 8:27 flemminghaflemmingha 1211 gold badge1 silver badge8 bronze badges 8
  • 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
 |  Show 3 more comments

1 Answer 1

Reset to default 6

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/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.

本文标签: query stringHow to get a URL parameter from a URL with getqueryvar