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 | Show 3 more comments1 Answer
Reset to default 6I 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
版权声明:本文标题:query string - How to get a URL parameter from a URL with get_query_var? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329033a2372662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$token
var? – cybmeta Commented Mar 19, 2018 at 9:10<?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:22function 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:06echo $_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