Frank Verhoeven

Webdevelopment Blog

  • Home
  • Forums
  • Contact

Home ⟩ Using Sessions in WordPress

Quick Results

    Forums

    • FV Community News
    • General Discussion
    • FV Code Highlighter

    Recent Topics

    • jQuery error
    • How to translate?
    • Link for posts not working
    • Changing the font on the form?
    • Tags

    Using Sessions in WordPress

    Posted by Frank | Filed under PHP WordPress

    Yesterday, 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:

    Select Code
    1
    2
    3
    4
    5
    
    /**
     * 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). This prevents errors in WordPress/Plugins/Themes from breaking it.

    In your theme/plugin

    As some of people have suggested in the comments, using the wp-config file might not always be the best solution.
    Add the next piece of code to your functions.php or plugin file to enable sessions:

    Select Code
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    /**
     * 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.

    Share this:

    September 20, 2008

    103 thoughts on “Using Sessions in WordPress”

    ← Older Comments 1 2 3

    1. Oliver on November 18, 2011 at 8:17 AM said:

      Thanks a lot. It can be so simple.

      Log in to Reply
    2. Mike on November 19, 2011 at 1:43 PM said:

      Or use functions.php:

      if ( !session_id() )
      add_action( ‘init’, ‘session_start’ );

      Log in to Reply
    3. Bala on November 23, 2011 at 9:23 AM said:

      It’s working in local server.. but session has not set in remote server…. how to solve it.

      Thanks
      Bala

      Log in to Reply
    4. Erick on November 24, 2011 at 8:05 PM said:

      Oh man thxxxx…. I am brazilian and i was looking for the solution to the day. Sorry for google translate.

      Log in to Reply
    5. Mvied on November 25, 2011 at 5:35 PM said:

      You can place the code snippet in the functions.php of your theme, or create a plugin in wp-content/plugins and your change won’t be lost when upgrading.

      Log in to Reply
    6. Jake Broughton on December 2, 2011 at 2:07 PM said:

      @Mike Thanks, good solution

      Does the session not need to be closed afterwards?
      If so, how do I do that?

      Log in to Reply
    7. Karen Sullivan on December 6, 2011 at 7:39 AM said:

      I seem to be the only person where your solution does not work. Newbie here so it might well be operator error.

      I was using ob_start / if (session_id == ”) start_session in an include file used by all the pages – that didn’t work
      Changed the wp-config.php file per your suggestion. that didn’t work — removed by code from the include file and that didn’t work

      Running 3.2.1 with the wptdacaiberry theme

      Here’s the .htaccess file:

      # BEGIN WordPress

      RewriteEngine On
      # Options +FollowSymLinks
      RewriteBase /
      RewriteRule ^index\.php$ – [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]

      # END WordPress

      I apologize for pestering you with this. Please let me know where I might ask this question.

      Thank you

      Log in to Reply
    8. Karen Sullivan on December 6, 2011 at 4:12 PM said:

      In wp-includes/load.php, removed references to $_SESSION where $_GLOBALS[$k] is nulled and unset.

      Thank you

      Log in to Reply
    9. Danny on December 14, 2011 at 1:44 PM said:

      Thanks man.You are a star!Had spent hours trying to figure this out.Your solution works well.

      Log in to Reply
    10. Kelly on December 22, 2011 at 9:41 AM said:

      I can set my sessions fine and they work on that page but if I go to another page I loose them. I tried removing the reference to sessions in the load.php but that didn’t help. Is there something I am missing? Do I need to make Sessions global somewhere?

      Log in to Reply
    11. Peter Wooster on February 4, 2012 at 4:28 AM said:

      Hi Frank,

      I found this post and its comments to be very helpful. I’ve done some more research and have posted the results and a link to this blog post in http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/ I cover the aspects of using this in a plugin, destroying the session on logout and login, and problems associated with register_globals.

      Thanks for getting me started on a full solution to this.
      /peter

      Log in to Reply
    12. Peter Wooster on February 7, 2012 at 9:37 PM said:

      @Kelly, are both your pages on the same subdomain. note that forbear.com and http://www.foobar.com are different subdomains and therefore have different sessions. WordPress usually takes care of redirecting from one to the other, but that is one other possibility.

      /peter

      Log in to Reply
    13. Pieter on February 15, 2012 at 10:53 AM said:

      Hi Frank, your solution works perfect, but it’s better to add it trough the init action hook, since it’s just not ment to be in the config file.
      You can add it as a plugin or just directly in your functions.php.
      I’ve read your post first, but this post made me change it to an init action. You might want to update your post.

      Regards.

      Log in to Reply
    14. Neo on April 3, 2012 at 12:19 PM said:

      THAAAAAAAAAAANKS Man!

      Log in to Reply
    15. Peter Wooster on April 3, 2012 at 4:18 PM said:

      Hi Frank,

      I just wanted to let you know that I released a plugin that incorporates all this. It only starts the session if it’s not already started and destroys it at logout and login. It also provides functions to get and set session values. The plugin can be found at http://wordpress.org/extend/plugins/simple-session-support/ in the repository.

      Thanks again/peter

      Log in to Reply
      1. Frank on April 4, 2012 at 12:08 AM said:

        Hey Peter,

        This is a nice resource for developers!

        One tip for developers who intend to use it though: you should include it in your own plugin. Otherwise you will have to rely on people with absolutely no knowledge about these kind of things to have this plugin installed, which is generally not a very good idea.

    16. Peter Wooster on April 5, 2012 at 1:54 AM said:

      Thanks Frank,

      I agree, my simple plugins are best used as examples for developers. WordPress hasn’t been able to make dependencies work for themes, let alone plugins.

      /peter

      Log in to Reply
    17. KYB on May 22, 2012 at 3:31 AM said:

      Thank you so much!!!

      Log in to Reply
    18. Tung on July 11, 2012 at 3:55 PM said:

      Oh, thank you, i really need now. :D

      Log in to Reply

    ← Older Comments 1 2 3

    Leave a Reply Cancel reply

    You must be logged in to post a comment.

    Meta

    • Login
    • Lost Password
    • Register

    Categories

    • Internet
    • News
    • PHP
    • Web Hosting
    • WordPress
    • Wordpress Plugins

    Tags

    Community News FV Community News Hosting Internet News PHP Web WordPress Wordpress Plugins

    Home | Sitemap | Top


    Copyright © 2013 Frank Verhoeven