Change global values from templates

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

1 Answer
1

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(){
    session_start();
    $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']

Leave a Comment