Solved

Session Variables in Wordpress

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now