I would like to get the query_string of my page www.hytech.dk

Hi.

I would like to setup a page on my website which can grab a query string called token from the url.

It would look like this:

http://www.hytech.dk/tokensuccess/?token=foo

I therefore create a child theme and add the following function to functions.php as described by the wordpress codex.

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

add_filter( 'query_vars', 'add_query_vars_filter' );

get_query_var('token');

Then on the webpage I insert a raw content box and write like this:

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

Hoping that it would write the value of the token in the text. However I do only see written

Currently Browsing token On a static front page

not

Currently Browsing token foo On a static front page

as I was hoping.

How can I achieve this and how can I insert the token value into the database?

Thanks a lot for any help!

Best regards
Flemming

Hi there,

Thanks for writing in.

It’s a bit complex as you will have to deal with many features and restriction when creating your own query. I recommend contacting a developer on this one. But based on your provided code, it’s wrong. You shouldn’t call it like this,

<?php $token = add_query_vars_filter( 'token', $default = ''); ?>

Thanks!

Hi

Thanks for your response, do you know how I could call a hook like this?

I would also like to know if I can I make a php call like this at all with the raw content element?

Would be good to know before I start writing all sorts of code which then doesnt work because the feature isnt available.

Thx
Flemming.

Hello There,

For more information about query variables, please check it out here:
https://codex.wordpress.org/Function_Reference/get_query_var
http://endlessgeek.com/2014/02/wordpress-pass-extra-variable-url-parameter/

Please be advised that Raw Content element will only allow raw html codes. Any php code added will not work and will be displayed as text only.

Hope this helps.

Hi

Thx for your reply.

I tried adding

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

add_filter( 'query_vars', 'add_query_vars_filter' );

get_query_var('token');

to the functions.php file of my child theme and

if (get_query_var('token')) print "token = $token";

to the pro theme’s single.php file and put the same file in my child theme as described in

http://endlessgeek.com/2014/02/wordpress-pass-extra-variable-url-parameter/

However when I access

www.hytech.dk/?token=foot

then foot is not printed on the page. Isnt that what the code is supposed to do?

Is it possible to make php calls using cornerstone or how can this be done? With a shortcode maybe?

Thx and best regards
Flemming

Hi There,

Would you mind providing us with login credentials(by clicking on the Secure Note button at the bottom) so we can take a closer look? To do this, you can make a post with the following info:

  • Link login to your site
  • WordPress Admin username / password
  • FTP credentials

Thanks.

Hi,

I added a secure note above.

Thx

Hello There,

The line of code should be this:

<?php if (get_query_var('token')) print "token = $token"; ?>

I went ahead and changed it and it is now displaying:

Hope this helps.

Hi

Thx a lot this worked like a charm!

However, what I really want to achieve is to be able to insert this value into my database in order to grab tokens from users who go to the landing page and also include their username.

It means that if I have a URL like this:

http://www.hytech.dk/tokensuccess/?token=footi&username=myname

Then I would like to insert ‘footi’ and ‘myname’ into a table in my database.

I therefore updated my functions.php file to get both variables and it works great:

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

I then opened my page.php file in my pro.theme and added the following code which also works as it prints both values:

<?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"; }  ?>

I then try to modify the code so that instead of printing the values of the variables it inserts them into the database.

I went to my database and created a table called wp_token and then I changed the page.php file to this:

<?php if (is_page('tokensuccess')) { if (get_query_var('token' and 'username')) 

global $wpdb;
$wpdb->insert( 
    'wp_token',
    array( 'token' => $token ),
    array( 'username' => $username ),
    array( '%s' ),
    array( '%d' )  
);

Unfortunately nothing is inserted into the database when I go to

http://www.hytech.dk/tokensuccess/?token=footi&username=myname

Do you know how I could insert the variables into wp_token?

Thx for the help so far, was really struggling with the above.

Flemming

Hi There,

The code is wrong.

It should be like this:

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 ),
		    array( 'username' => $username ),
		    array( '%s' ),
		    array( '%d' )  
		);
	}
}

Regards!

Hi there

Thx for the correction … I updated page.php to this:

<?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 ),
	        array( 'username' => $username ),
	        array( '%s' ),
	        array( '%d' )  
	    );
    }
}

?>

then I go to the page

http://www.hytech.dk/tokensuccess/?token=foo&username=bar

and I get the variables printed in the bottom of the page as can be seen below

However when I check my database it is empty. Did I set up the table incorrectly or do you know why the data is not inserted?

Thx
Flemming

Hi There,

Please update the code to this:

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

Let us know if this code works for you.

1 Like

It worked, got some values in my table now. Thx a lot for the help! It will make my customer handling so much easier!!

1 Like