Solved

Session Variables in Wordpress

Posted on 2014-10-27
5
818 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

734 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