WordPress session start front end

I am trying to create plugin in wordpress.. Plugin data direct to wordpress front end and I want stat wordpress session in front end using the plugin.

I tried using this but this is not working, can someone help me

function register_session()
{
  session_start();
}
add_action('init', 'register_session', 1);

Related posts

Leave a Reply

1 comment

  1. Add this to your plugin or themes’ functions.php template:

    function register_session(){
    if( !session_id() ) //checking if session already exists
        session_start();
    }
    add_action('init','register_session');
    

    Then to set the session data use something like this:

    $_SESSION['username'] = 'some data';