WordPress.org

Ready to get started?Download WordPress

Forums

Okay to use $_SESSION in plugins? (3 posts)

  1. djpushplay
    Member
    Posted 6 years ago #

    In the process of creating a plugin, I needed to use a $_SESSION in order to save info but I noticed that session_start() is not used in WordPress.

    So I started to think that WordPress does not want you to use $_SESSION. If this is the case, what is the proper way to go about doing something equivalent.

    If it's okay to use $_SESSION, should I put the session_star() in wp-config.php or in the plugin itself?

    Sorry in advance for being a WordPress newbie but hopefully I'll be able to repay the community in the future when I get a hang of this.

  2. jeremyduffy
    Member
    Posted 6 years ago #

    WordPress DOES use session start.... either that or it was me that did it...

    Either way, I've used it and it will work fine. Mine is in my header.php file. Since that's loaded first (before the rest of my stuff anyway) and my plugin is a content filter, I can use session variables in my plugin without starting the session there (since it was already started in my header).

    So far, no problems. You can always try it an see. In fact, try this:

    Before you add anything, put a print_r($_SESSION) statement in your code to see if there's already a session running. If you can print session variables from your plugin, then you can use them there.

  3. djpushplay
    Member
    Posted 6 years ago #

    Thanks jeremyduffy. I put the session_start() at the start of my plugin and it works fine but without it, it doesn't work. In any case, it's all good.

Topic Closed

This topic has been closed to new replies.

About this Topic