wordpress以编程方式注销其他地方

时间:2016-05-22 20:45:58

标签: wordpress

如何从wordpress中的其他地方注销? [不是wp_logout(),因为它只会破坏当前会话]

我使用了这个功能,但它不起作用:

WP_User_Meta_Session_Tokens::destroy_other_sessions();

2 个答案:

答案 0 :(得分:1)

您必须首先获取用户ID,然后获取令牌,保护它并销毁其他会话。这是一个有效的例子:

global $wp_session;
$user_id = get_current_user_id();
$session = wp_get_session_token();
$sessions = WP_Session_Tokens::get_instance($user_id);
$sessions->destroy_others($session);

答案 1 :(得分:0)

这是一个与 init 操作挂钩的工作版本。

/**
 * Destroys all sessions for this user except the one with the given token (presumably the one in use).
 */
add_action( 'init', 'destroy_all_other_current_user_sessions' );

function destroy_all_other_current_user_sessions() {

    $manager = WP_Session_Tokens::get_instance( get_current_user_id() );

    $manager->destroy_others( wp_get_session_token() );

};