Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use session variables in wordpress?

I have the following plugin: http://wordpress.org/support/plugin/wp-session-manager

I cannot work out how to use the session variables in WordPress. From what I understand by reading, this is how it should be used:

I have the following on page one (page 1):

global $wp_session;
$wp_session['loggedIn'] = 15;

echo $wp_session['loggedIn'];

On the first page, the session variable is working but on the second page, the session variable is not working. Can anyone suggest me where I am going wrong?

Thanks.

like image 588
user1978592 Avatar asked May 22 '14 15:05

user1978592


People also ask

How do session variables work?

Session variables are special variables that exist only while the user's session with your application is active. Session variables are specific to each visitor to your site. They are used to store user-specific information that needs to be accessed by multiple pages in a web application.

Can I use session in WordPress?

WordPress Core does not use sessions. All "user state" is managed via cookies. This is a Core design decision. However, some plugins or themes will use session_start() or PHP's $_SESSION superglobal.

How do I use session plugins in WordPress?

From your WordPress dashboardVisit 'Plugins > Add New'. Search for 'Sessions'. Click on the 'Install Now' button. Activate Sessions.


1 Answers

Replace:

global $wp_session;

With:

$wp_session = WP_Session::get_instance();

Make sure you add $wp_session = WP_Session::get_instance(); before you try to echo the variable on page 2.

like image 173
Nathan Dawson Avatar answered Oct 29 '22 15:10

Nathan Dawson