• So I have a little hack I made to add the user’s PHPSESSID to the wp_usermeta table when a user logs in (which is working perfectly). I’ve been trying for several hours to delete the same info when a user logs out, and can’t seem to get the dang thing to work. I’m wondering now if the wp user object isn’t available anymore after you click logout? I read elsewhere that I should be able to use wp_clear_auth_cookie(), but no luck there either. Any insights would be great.

    function create_session_id($user_login){
    
        if(!session_id()) {
                	session_start();
    		$user_obj = get_user_by('login', $user_login );
    		$user_id = $user_obj->ID;
    		$session_id = session_id();
    		add_user_meta( $user_id, 'session_id', $session_id);
    	}else{
    		$user_obj = get_user_by('login', $user_login );
    		$user_id = $user_obj->ID;
    		$session_id = session_id();
    		add_user_meta( $user_id, 'session_id', $session_id);
    	}
    }
    add_action('wp_login', 'create_session_id');
    
    function delete_session_id(){
    
    		$current_user = wp_get_current_user();
    		$user_id = $current_user->ID;
    		delete_user_meta( $user_id, 'session_id', '');
    }
    add_action('wp_clear_auth_cookie()', 'delete_session_id');
Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion

    (@diondesigns)

    FYI…WordPress added session support in 4.0, so your code is unnecessary. You’ll find the session information in the session_tokens meta key for each user. The session ID is the array key, the remaining information is in the array values. You can view the (serialized) format using phpMyAdmin on the _usermeta table.

    Thread Starter amybill1978

    (@amybill1978)

    Hi and thanks for the reply. I needed the session ID in raw format to share it with a .Net application – and also needed the session ID to be removed once a user logs out (which WP doesn’t seem to do for some reason). Its a weird setup, but I actually figured out how to fix my own problem. I just changed ‘wp_clear_auth_cookie()’ to ‘clear_auth_cookie’ and now it works. Go figure!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘delete_user_meta function with wp_clear_auth_cookie?’ is closed to new replies.