Using Sessions in WordPress
Posted by Frank on September 20, 2008Yesterday, when I was creating a new plugin for my friend Stefan Vervoort, I needed sessions to work. Unfortunately, WordPress doesn’t support them?!
I searched the whole source code of this great piece of blogging software, and not on one single line I found even one session. Also the session_start() function is not called, but I still needed my sessions to work ๐ .
So I started searching Google for a fix of my problem. I found a lot of people asking the same question: “Why do sessions not work in WordPress?” Finally I found a solution to fix this little issue and guess what, it is a simple one.
The Solution
The only thing you have to do is call session_start(); before any output is send to the client. Now you might think: Nice, but what happens when I upgrade my WordPress installation to the latest version? Well, yes, your changes will be lost. That is the reason why we first should think of where to add these changes..
Normally upgrading your installation will replace all files, except one. Yes, it is the wp-config.php file. And even better, there isn’t send any output to a client yet, when this file is loaded.
So, we add the next lines of code to our wp-config.php file:
<?php |
|
/** |
|
* Enable sessions |
|
*/ |
|
if (!session_id()) { |
|
session_start(); |
|
} |
And sessions are enabled on your blog!
I think the best place to add these lines is at the top of the config file, so immediately after the php starting tag (<?php).
In your theme/plugin
As a lot of people have suggested in the comments, using the wp-config file might not be the best solution.
Add the next piece of code to your functions.php or plugin file to enable sessions:
<?php |
|
/** |
|
* init_sessions() |
|
* |
|
* @uses session_id() |
|
* @uses session_start() |
|
*/ |
|
function init_sessions() |
|
{ |
|
if (!session_id()) { |
|
session_start(); |
|
} |
|
} |
|
add_action('init', 'init_sessions'); |
I hope this will help you out when facing the same problem.
Great tip.
This is the first time I see someone using the session_id to check if the session is already started. And I must say, this is a much better solution than all the other.
Thanks again!
great hint, thanks!
i placed the code in the “functions.php” of my theme, wich works fine to…
if(!session_id())
session_start();
Just what I needed for my keywords ROI tracking project.
Thanks very much.
Pingback: Wordpress : utiliser les sessions – Synapse Studio
I am going to write a shopping cart
and that’s what i just needed.
Awesome solution!
Thanks a lot for sharing it on your site.
Thank a lots!!! its really helpful to my project.. i am developing a booking system using wordpress..
Thanks a lot))
Pingback: Wordpress – Enable PHP Session « Eureka!
While your solution works just fine, I would advise against it from a WordPress best practices point of view. It is not great practice to add a session starter to a core file because another plugin author might add it somewhere else, and then all sorts of badness might happen.
The best solution is to use the wordpress settings API (http://codex.wordpress.org/Settings_API), $_GET, $_POST if needed. Obviously you needs may call for session_start, in which case I advise using an action hook. If you place the following in your plugin it should accomplish the same thing as putting it in the wp-config file.
add_action(โinitโ, โmy_functionโ);
function my_function() {
if (!session_id())
session_start();
}
If you are unfamiliar with hooks and filters, the following link is a good place to start: http://codex.wordpress.org/Plugin_API
Hi.
I spent a couple of hours figuring out how I can use session variables in my plugins since printing out session displayed everything in an external php file but nothing in my WP page. Brilliant solution.
Many thanks!
Pompos
Pingback: The Girl in the Red Sweater » Blog Archive » Using sessions in WordPress
Pingback: Sessions and WordPress - Blankfield.net
Thank you for the session information in blog.
Thank you for this. It just solve my problem.
๐
This one really works!!! After a long days of researching finally found the answer ๐
Thank to this article.
– juks
Pingback: Wordpress e session_start() | Marcello Marchese
Thanks mate, after pulling my hair out trying different things with wp_register_globals_off() it turns out to be as simple as you say, sessions have not been started
Thanks
Wolf
Thanks for this helpful info..as i m new to wordpress ๐
I’ve been battling this for some time. I put the session start in my plug in code, but always had random problems. This was just the ticket. THANK YOU!!!
unset($_SESSION[SES_NAME]);
OR
$_SESSION[TYPE_LANG]=””;
Using this code we can unset session .
I was able to start the session but how to Unset sessions?
THANK YOU SO MUCH! You saved my day.
Hi all! I am developing a theme for commercial use and require sessions to run to add some functionality that is needed.
At the moment wp_unregister_GLOBALS() seem to be a great problem for me, since modifying wp-setting.php manually is not an option. Has anyone got any ideas on how to add SESSIONS global variable to a $noUnset list, by coding only inside my theme folder? Or maybe desable wp_unregister_GLOBALS()? Or perhaps deactivate register_globals in php setting file of the server? But remember I am limited to doing that by code only from inside my theme folder. Any advise on this will be much appreciated, I’ve been trying to find a solution for a week now and getting very frastrated at wordpress…
Cheers
Max
Nice share. Thanks for posting!
You saved my day. Thanks for the help ๐
function cp_admin_init() {
if (!session_id())
session_start();
}
add_action(โinitโ, โcp_admin_initโ);
Its working….
thanks all.
Frank THANK YOU SO MUCH!
I was trying to figure out why I couldn’t keep the session between my main site and my blog and you’ve solved it for me!
Cheers!
But will you ask your plugin users to do the same? o.O
Why don’t put it inside your plugin, just before you need the session data?
If it’s not set, you set them and consider the user just got to the site. If ID is available, just use it normally because it was started somewhere earlier.
Hello Frank,
Thanks for the session information that is really useful.
I am not quite familiar about how to use session.
I wondered if you could get the user name from each user login to the website.
Thanks.
john
I tried all methods on this page and found that Andy Potanin’s (October 5th, 2009 at 2:27 am) solution worked the best. Just add_action() in your plugin initialize file.
This works fine with the built in session function but does not for me when using a custom session handler -storing session in a db.
I am including it on the top of wp-config.php as with this example.
I’ve tried the wp-settings.php adjustment for variables to unset and am really scratching my head on this one…. any ideas?
thanks!
there is no need for
if (!session_id())
u must use only session_start();
I just inserted session_start() in my plugin file just below the plugin details and it works ๐
thank u sir.
i am searching for this since 3days
Mine session_start still not work.
I got a script include in a wordpress page, but it seems that the session stay empty.
If i use the script without include it in wordpress than it works.
Is there someone who wants to help me ?