Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Session Variables in Wordpress

I'm trying to create session variables in Wordpress and have discovered that Wordpress does not really do this. I found a plugin Called WP_Sessions and it is said to help create sessions.  How you use it is as follows:
$wp_session = WP_Session::get_instance();
$wp_session['user_name'] = 'User Name';                            // A string
$wp_session['user_contact'] = array( 'email' => 'user@name.com' ); // An array
$wp_session['user_obj'] = new WP_User( 1 );                        // An objec

Open in new window


OK, I want to store a field from a form.
<input type="text" name="addon-28-bicycle-info[manufacturer]" id="addon-28-bicycle-info[manufacturer]"  value="<?php  if(isset($_POST['addon-28-bicycle-info']['manufacturer'])) {echo $_POST['addon-28-bicycle-info']['manufacturer'];}  ?>" >

Open in new window

How do I store that field as a session variable after the form is submitted?
Here is a link to the plugin:
https://wordpress.org/plugins/wp-session-manager/faq/
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Granlund

ASKER

The can I do something like the following to pass the variable from page to page?
$_SESSION['bv'] = $_POST['bike_value'];
$_SESSION['manu'] = $_POST['addon-28-bicycle-info[manufacturer]'];
Do those  need to be in the head?
The head of what?  PHP code is independent of HTML structures.  Traditionally, we have constructed PHP pages like so:

<?php
 blah; 
 blah; 
 blah;
?>
<html>
<head></head>
<body>
<?php echo $somevar ?>
</body>
</html>

Open in new window


But there's no difference between that and this:

<html>
<head></head>
<body>
<?php
 blah; 
 blah; 
 blah;
 echo $somevar; 
?>
</body>
</html>

Open in new window


So long as the variables are declared before they are used, you should be okay.