0

I would like to change a session variable which will change another variable in two different places. One variable determines what posts the user will see which is in my custom post archive file(archive-spark.php) and the other in my (functions.php) file which I am using in a Gravity forms hook which determines which category the form will post to. The gravity form is a pop up on all pages.

All is working fine when I am on custom post type page(archive-spark.php), the form I created changes the variables in both but when I reload the page or go to another page the default variable I have set loads again. I can see why it does this as I have the session in a 'if(isset)' but I just don't know how to hold the variable as session variable after my form submission.

I am using this plugin for my session variables: http://silvermapleweb.com/using-the-php-session-in-wordpress/

Here is the code: archive-spark.php

<?php
//this is default variable if nothing is submitted with my form
$the_user_garden = "gardens"; 
?>

<?php
//this is the form that will change the variable
if(isset($_POST['submit'])) 
{ 
$the_user_garden = $_POST['sub_garden_1'];
}
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<input type="text" name="sub_garden_1" value="sub_garden_1"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>

<?php
//this put the new variable value   from the form as session variable
$_SESSION['myKey'] = $the_user_garden; 
?>

<?php
$author_id = get_current_user_id( );
//this is where I am using the changes variable
if( have_rows($the_user_garden, 'user_'.$author_id) ): ?> 

<?php 
//and here too
while( have_rows($the_user_garden, 'user_'.$author_id) ): the_row(); ?>

functions.php

<?php
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

function myStartSession() {
if(!session_id()) {
    session_start();

}
}

function myEndSession() {
session_destroy ();
}

?>
<?php
// this is where I need the variable for gravity form(pop up)   
 if(isset($_SESSION['myKey'])) {
$the_user_garden = $_SESSION['myKey'];
} else {
$the_user_garden = "gardens";

}
?>

<?php
//this is where I am using variable
$author_id = get_current_user_id( );
if( have_rows($the_user_garden, 'user_'.$author_id) ): ?>

Basically I need a way for session variable to be stored through out the website on all pages and if I submit my form for that variable to be held.

Thanks in advance..

1

Not sure if anyone understood my problem but if you did and are having something similar I will post how I figured it out.

I simply had to add a session_start(); on my form..

<?php
session_start(); //see here
//this is the form that will change the variable
if(isset($_POST['submit'])) 
{ 
$the_user_garden = $_POST['sub_garden_1'];
}
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<input type="text" name="sub_garden_1" value="sub_garden_1"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>

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.