Just looking for some advice on something that is bugging me. Please bear in mind I am a PHP noob :-)

I am passing a variable in the url to my WP index page like so:

<a href="<?php bloginfo('url');?>/?do=thing">Thing</a>

I am the catching that variable, and using it to show content like so:

<?php $do_that = $_GET["do"]; if($do_that == 'thing') : ?> etc

Naturally I am getting debug errors when the variable is not passed :-)

"Undefined index: do"

My question is what am I missing wrong? Obvious I spose?

Also is this bad practice?


The error is occurring as the $_GET array doesn't have the item $_GET['do'] in it. Therefore it throws this notice. To check if something exisits in the array try:

if( isset( $_GET['do'] ) )
   $do_that = $_GET['do'];
   $do_that = '';

or a cleaner method would be to use short hand notation

$do_that = ( isset( $_GET['do'] ) ) ? $_GET['do'] : '';
  • You will still need to validate the data, remember to NEVER trust a users input. Check out the article codex.wordpress.org/Data_Validation there are some great helper functions that can clean the data :)
    – Cristian
    Jul 18 '11 at 12:44

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.