Search code examples
phpwordpresssessionsession-variables

WP_Session plugin not working


So I've Googled and read myself to death and can't find a solution to this problem.

I'm using the WP Session Manager plugin to use session data. We need a very specific social login and register flow. We have a Wordpress site (version 4.2.1) at www.domain.com and our web app is located at app.domain.com. Now if you log in or register on the WP site, you are logged in / registered on the app (not the WP site).

I'm writing a custom plugin for this as the options out there does not fulfill my needs.

My issue comes in with accessing the session data between functions. My flow is as follow (using Google as an example):

User clicks Google+ login, popup with Google Authentication pops up, user gives permission, callback url is www.domain.com/google_callback. The page google_callback has a shortcode called google_callback which then calls the function. Everything in that function works and I get the data back properly. With all the Google data handling stripped away, that function looks like this:

// social.php plugin file
function google_callback() {
    $wp_session = WP_Session::get_instance();
    // Get Google data all happens here...
    $user['name']     = $userObj->givenName;
    $user['surname']  = $userObj->familyName;
    $user['email']    = $userObj->email;
    $user['verified'] = $userObj->verifiedEmail;
    $wp_session['registeruser'] = $user;
    $wp_session['registertype'] = 'Google';
    wp_redirect(home_url('social-register'));
}

Now if I var_dump($wp_session); just before wp_redirect, I get all the Google data correctly.

The social-register page has a shortcode that builds a form and pre-populates it with the retrieved data. This is that function:

// theme functions.php file
function build_registerform($args,$content=null) {
    $wp_session = WP_Session::get_instance();
    var_dump($wp_session);
}

At this var_dump, $wp_session is an empty WP_Session object.

I have tried changing all the $wp_session = WP_Session::get_instance(); to global $wp_session with no luck.

Does anyone know what I'm doing wrong?


Solution

  • Well i think in your social.php you will first have to make $wp_session global out side any function. So on the top of the social.php, after including scripts. add this

    global $wp_session = WP_Session::get_instance();
    

    Then later on in the google_callback function just use

    global $wp_session;
    

    Similarly in your theme functions.php

    function build_registerform($args,$content=null) {
        global $wp_session;
        var_dump($wp_session); //session is empty right now
    }