Skip to content
Advertisement

How to use session without using $_SESSION in wordpress

I am trying to create a session without using php $_SESSION variable and without letting user to log in on the website. I created a form and once a visitor/guest user submit the form, I want to store his data in a session variable. I found that using $_SESSION is not a good practice in wordpress. I looked around some tutorials and found to use WP_Session. This also seems not working for me.

Here is the code I am using:

global $wp_session;
$wp_session['visitorInfo'] = 'some_text_here';

When I am requesting it on another page it gives me nothing(blank). Even if I try to print $wp_session variable, it also give me blank. I also tried with $wp_session = WP_Session::get_instance();, but it returns me error that Class WP_Session not found may be because I am using namespace for the class in which I am calling this method.

I also tried to

add_action('init',array($this,'start_session'));
public function start_session(){
  if (!session_id()) { session_start();}
}

but its not working.

My website is hosted on wpengine and it doesn’t allow creating custom cookies. I am trying to figure out how wordpress create their session when user login to the website. I have been looking for the solution from last 2 days but didn’t found any working solution.

Can you please suggest me how I can achieve this.

Many many thank in advance!

Advertisement

Answer

Use the init hook to session_start() is working fine on normal server without WP Engine.

Example code:

<?php
/**
 * Plugin Name: Session plugin.
 */


add_action('init', 'sesp_init');
function sesp_init()
{
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }
    $_SESSION['hello'] = 'world';
}


add_action('template_redirect', 'sesp_template');
function sesp_template()
{
    echo 'hello ' . $_SESSION['hello'].'<br>';
}

It say hello world on the front pages.

WP_Session

The class WP_Session is not WordPress core class. I couldn’t find this class anywhere in WordPress core files except WP_Session_Token but that is not for session usage.

The class WP_Session is from WP Session Manager plugin (refer from this question). You have to install that plugin to access WP_Session. Otherwise this error will be occur.

Class WP_Session not found

Use Transients.

From the document.

WordPress Transients API, which offers a simple and standardized way of storing cached data in the database temporarily

So, you can use it as alternative if session_start() is not really work and you don’t want to install other plugin.

Example code:

add_action('init', 'sesp_init');
function sesp_init()
{
    set_transient('sesp_hello', 'moon', 60*5);// 5 minutes.
}


add_action('template_redirect', 'sesp_template');
function sesp_template()
{
    echo 'hello ' . get_transient('sesp_hello') . ' (use transient).<br>';
}

You don’t need to hook into init to set transient. The code above is for example. The third argument in set_transient() is expiration in seconds.

Transient Warning!

However, the transient is not for one visitor like the session. You have to set and check properly that the transient you are getting and setting is match for a person.

WP Engine

From their support page.

The biggest problem this presents is due to the unique session IDs. Unique IDs effectively bust cache and causes every session to become uncached. This will cause serious performance issues for your site.

You may switch to use PHP cookie or disable WP Engine cache. This was answered here.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement