'Create function to logout in wordpress

in the wordpress functions.php page, I enabled Session variables like this:

session_start();
$_SESSION['usuarioId'] = $user_ID;;
$_SESSION['usuarioNome'] = $user_identity;
$_SESSION['email'] = $user_email;
$_SESSION['login'] = $user_login;
function register_my_session(){
    if(! session_id()) {
        session_start();
    }
}

When logging out with the wordpress logout function, I don't destroy the sessions of the pages where I use them. I no longer have access to the wp-admin page, because I logged out, but on the pages where I have these sessions, the pages still have access.

I have this problem, I need to create a function to log out of the sit

Can you help?



Solution 1:[1]

Maybe try:

function wp_destroy_current_session() {
    $token = wp_get_session_token();
    if ( $token ) {
        $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
        $manager->destroy( $token );
    }
}

Source: wp_destroy_current_session()

Solution 2:[2]

You probably want to use the wp_logout. Per the documentation:

Fires after a user is logged out.

add_action(
    'wp_logout',
    function() {

        // Destroy PHP's relation to the session
        session_destroy();

        // Unset the global session variable, just in case they are used elsewhere
        $_SESSION = [];
    }
);

If you are using a theme, this code can be placed in functions.php, or anything that that file includes.

Make sure to read the documentation on session_destroy to understand that it might not behave exactly as you expect, depending on when you call it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DᴀʀᴛʜVᴀᴅᴇʀ
Solution 2 Chris Haas