How to Enable Sessions in WordPress

The Issue

Let's say you want to use something like this in your theme or plugin:

$_SESSION['my_var'] = "Hello World!";

It might be actually not working.

The Solution

Paste de following line in your functions.php file of your theme:

if ( !session_id() )
add_action( 'init', 'session_start' );

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.