Solved

Session Variables in Wordpress

Posted on 2014-10-27
5
663 Views
Last Modified: 2014-12-03
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/
0
Comment
Question by:rgranlund
  • 3
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 40406865
You don't need a plugin and its overhead for this.  Just add the following code to functions.php:

add_action('init', 'myStartSession', 1);
function myStartSession() {
    if(!session_id()) {
        session_start();
    }
}

Open in new window


This will allow sessions in WordPress and standard PHP notation from there.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40407401
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]'];
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40407624
Yes
0
 
LVL 7

Author Comment

by:rgranlund
ID: 40408788
Do those  need to be in the head?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40408892
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question