WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to check for a php variable in each of my template in order to assign it a different styling but the value seems to disappear, can you please help, I already spent hours trying to figure this out. I know there is something small I'm missing.

So here is everything:

I have a page a which links to b and pass a value in the URI as www.b.com/?value=h

now the b file receive the value and change the global variable's value to the one passed in the URI, then redirect back to the page.

This is the code in my b file:

$server = $_SERVER["REQUEST_URI"]; $valuePos = stripos($server, "=");

$nodes = substr($server, ($valuePos + 1)); $endPos = stripos($server, "e"); $gender = substr($server, ($valuePos + 1), ($endPos));

$valuePos = stripos($nodes, "-");

$redirecturl = substr($nodes, ($valuePos + 1));

header( 'Location: '.$redirecturl );

$gender is the global variable that I have declared in function.php, I have set its default value to neutral.

Now when I try to access $gender from another template it just displays the default value (neutral). Please help

share|improve this question
up vote 0 down vote accepted

I think you could simplify much of what you are doing just by using the global $_SESSION variable.

try something like this:

add_action('wp_head', 'your_session_variable' );

function your_session_variable(){
    $gender = isset($_GET['gender']) ? $_GET['gender'] : $_SESSION['gender'];
    $_SESSION['gender'] = $gender;

Now to access the session variable from the template make sure to use session_start() before grabbing the value from $_SESSION['gender']

share|improve this answer
Hi Douglas.Sesar, thanks alot for your quick response, that was a huge help. – rodrigue Sep 17 '12 at 14:30
No problem, glad I could help. – Douglas.Sesar Sep 17 '12 at 18:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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