WordPress does not use sessions to hold data. It is a stateless application. This means that if you want to use sessions in your plugins or custom modifications of WordPress, you may need to do a little hack to enable sessions.
Edit your wp-config.php file (located at the root of your blog) and add the following line at the beginning of the file:
session_start();
Now you can use sessions in WordPress. Remember that for most plugins, instead of using sessions to store data you can pass variables in URLs and hidden fields or use cookies, and that is what WordPress encourages.
Yes, I’m surprised that WP doesn’t use sessions either. I actually found that using a hook is much better for plugin and themes so you then don’t have to worry about having the user modify their wp-config.php file.
Here’s a simple function you can add in your functions.php file that will start a session automatically. Then you don’t need the wp-config.php session_start(); code.
function cp_admin_init() {
if (!session_id())
session_start();
}
add_action(‘init’, ‘cp_admin_init’);
Thanks David,
I have been battling with sessions for a week now, am so glad i came across your code, it helped alot, am trying to build a member area in my wordpress blog, because there is some information that i want to display only to registered users , so that’s where the sessions come in. please post some links to sites that i can read more about this.
Otherwise thanks
why they don’t use session? what do they use, cookies?
how they know if you are still logged in. using cookies?
Great share. Thanks a lot. Really saved my day!!
its still not working for me. what I can be doing wrong????
Any help would be appreciated.
Thanks,
Arpita
function login_to_wp($u_name){
require(‘wp-blog-header.php’);
$user_login = $u_name;
$user = get_userdatabylogin($user_login);
$user_id = $user->ID;
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
do_action(‘wp_login’, $user_login);
}
function logout_to_wp($u_name){
require(‘wp-blog-header.php’);
$user_login = $u_name;
$user = get_userdatabylogin($user_login);
$user_id = $user->ID;
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
do_action(‘wp_logout’, ”);
}
login_to_wp($u_name);
}else{
logout_to_wp(”);
}
Thank you very much! This was very helpful for my own project too!
I needed to integrate WP in my own custom CMS because WP can’t handle things that my CMS does. So I have WordPress handling the blog/news part of my site. Instead of my admins having to login twice (in my CMS and WP) they can now login one time in my CMS using the login/session structure of WP applied to my own CMS.
It was so useful thaaanks
It does not work for me, too.
Have any other help?
Thanx for the tipp with the init hook, for session_start() the wp_head doesn’t work!!!
What could be the problem,so session work on one wordpress blog,but not on another,any idea how to solve this?
Thanks alot – your answer solved all my problems after several days srugtgling
[...] http://diovo.com/2009/10/using-sessions-in-wordpress/ [...]
[...] Using Sessions in WordPress [...]
I recommend to use
if (!session_id())
session_start();
}
It works for me.
Hi,
I am looking at various load content (video in this case) “once per session”. Need help. Hopelessly hapless.
Well another reason of sessions not working properly could be a plugin.
Yes it is possible if you have two such plugins installed which are starting session at a same time and it can cause them to work improperly.
A few days back i was having the same problem running shopp plugin on my site and i was unable to checkout because of “empty shopping cart” error. there was something wrong going with session variables and it was unable to store cart items then after researching a lot , going through various forums and digging into this problem i came to know after 15 days of continuous frustration that it was my event-manager plugin which was creating all the problem, infact not whole plugin but the em-notices.php file inside classes folder. which was not in my use so what i did is just renamed that file and checked my site again and everything was working perfectly fine
so having two or more plugins starting session at the same time can cause problem in session variable
Thanks Man, that worked for me.I wasted my full day googling around and finally got your article. But I am happy that finally it worked for me.. Just one question, you mentioned that WordPress encourages to use cookies and not sessions, any specific reason for that?
Thanks,
Nixy