Solved

Session Variables in Wordpress

Posted on 2014-10-27
5
712 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

733 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