I'm putting up a site using Wordpress and I'd like to piggyback on its sessions. But I'm not finding any plugins, or even documentation. Any suggestions or references before I start hacking it?
Note: I'm asking about if and how WP uses standard PHP sessions itself, not how to add PHP sessions e.g. using session_start(). Apparently any state WP maintains is accomplished by other means. So if I want to use PHP sessions I need to add and maintain it myself entirely, using techniques like those in the thread.
Thanks all!
When a user logs on to a WordPress website, a session is created. The details of the session are stored in the WordPress database, specifically in wp_usermeta table.
WordPress doesn't use sessions, that's why your session variables aren't working. As a matter of fact, if certain variables are defined, WordPress will actually destroy $_SESSION to keep itself stateless. But if you really want to use sessions, try adding session_start() at the beginning of your wp-config. php file.
To allow sessions you simply have to insert _SESSION into the array. The final code will be: $noUnset = array('_SESSION','GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');
Wordpress session functions:wp_session_encode() – write session data out to a serialized string. wp_session_regenerate_id() – change the ID of the current session to a new, random one. wp_session_start() – start the session and load data based on the user's cookie.
It's a very bad idea to modify WP Core files for the ability to use sessions. The best way I've found is to call the session_start()
from init
action hook.
function kana_init_session() { session_start(); } add_action('init', 'kana_init_session', 1);
You can place it in functions.php
file of your theme.
Detailed article can be found here: http://www.kanasolution.com/2011/01/session-variable-in-wordpress/
WordPress doesn't appear to call session_start()
because it wants to be stateless and if register_globals
is defined, it automatically destroys your $_SESSION
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With