钩子文档

wp_logout

💡 云策文档标注

概述

wp_logout 是一个 WordPress 动作钩子,在用户通过 wp_logout() 函数登出后触发,主要用于执行登出后的自定义操作。

关键要点

  • 触发时机:用户使用 wp_logout() 函数登出后,在 wp_clear_auth_cookie() 函数调用之后执行。
  • 参数:$user_id(整数),表示被登出用户的 ID。
  • 版本历史:从 WordPress 1.5.0 引入,5.5.0 版本添加了 $user_id 参数。
  • 相关函数:wp_logout() 用于登出当前用户。

代码示例

// 示例:使用 wp_logout 钩子删除用户登录时存储的瞬态数据
add_action('wp_logout', 'my_custom_logout_action');
function my_custom_logout_action($user_id) {
    // 删除与用户相关的瞬态数据
    delete_transient('user_' . $user_id . '_data');
}

注意事项

  • 此钩子仅在用户通过 wp_logout() 函数登出时触发,其他登出方式可能不会调用。
  • 确保在回调函数中正确处理 $user_id 参数,以避免潜在的错误。

📄 原文内容

Fires after a user is logged out.

Parameters

$user_idint
ID of the user that was logged out.

More Information

The wp_logout action hook is triggered when a user logs out using the wp_logout()  function. The action is executed after the wp_clear_auth_cookie()  function call.

Source

do_action( 'wp_logout', $user_id );

Changelog

Version Description
5.5.0 Added the $user_id parameter.
1.5.0 Introduced.

User Contributed Notes