I've spent a bit of time sorting out the use of PHP sessions in WordPress. I've posted a discussion of the issues on my blog at http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/
For those who just want the bottom line here it is. Put the following code in your plugin or theme code:
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
if(!session_id()) {
session_start();
}
}
function myEndSession() {
session_destroy ();
}
Do not put the session_start in wp-config.php if there is any chance that register_globals is set in your PHP system.
/peter
I have published this code as plugin Simple Session Support.
There are other session plugins out there, but they do not clean up at logout.
/peter
kishores
Member
Posted 10 months ago #
My php session unset due to wp_head()in header.php.My session works fine without wp_head(). Is there any conflict between session and wp_head?
beytar
Member
Posted 6 months ago #
There is a powerful plugin for sessions here: WP Sessions Plugin
That looks like a useful plugin for those who need all that functionality. However it is a CodeCanyon premium plugin and as such doesn't belong here.
lubchik
Member
Posted 4 months ago #
Thank you!!! I was really helpful!
tcreary
Member
Posted 4 months ago #
I installed and activated the plugin but I cannot pass $_SESSION variables yet. Is there anything else required to initiate the add_action function?