How to store and receive variables in WP sessions?

Sessions aren’t enabled in wordpress by default, if you want to activate php sessions add this at the beginning of your functions.php:

if (!session_id()) {
    session_start();
}

You now can use $_SESSION['your-var'] = 'your-value'; to set a session variable. Take a look at the PHP documentation on sessions.


Update:

There was a second answer, which, in my mind, had a value too – unfortunately it got deleted, I’m re-adding the information here. The answer was referring to WP Session Manager a plugin written by @eamann as a alternative solution.
I read some things about the plugin, but never used it because – so far – I’m sticking with PHP sessions – never had the problem that I couldn’t use them. This is a statement/comment by the plugin author himself I found about some possible advantages.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)