The wp_logout WordPress PHP action fires after a user is logged out.
Usage
add_action('wp_logout', 'your_custom_function'); function your_custom_function() { // your custom code here }
Parameters
- $user_id (int): ID of the user that was logged out.
More information
See WordPress Developer Resources: wp_logout
Examples
Redirect user after logout
Redirect users to the homepage after they have logged out.
add_action('wp_logout', 'redirect_after_logout'); function redirect_after_logout() { wp_safe_redirect(home_url()); exit; }
Log user logout events
Log user logout events for security or auditing purposes.
add_action('wp_logout', 'log_user_logout'); function log_user_logout() { $current_user = wp_get_current_user(); error_log('User ' . $current_user->user_login . ' has logged out.'); }
Clear user-specific cache after logout
Clear user-specific cache when the user logs out.
add_action('wp_logout', 'clear_user_cache'); function clear_user_cache() { $user_id = get_current_user_id(); wp_cache_delete("user_meta_{$user_id}", 'user_meta'); }
Send a notification after user logout
Send a notification to the admin when a user logs out.
add_action('wp_logout', 'send_logout_notification'); function send_logout_notification() { $current_user = wp_get_current_user(); $subject = 'User Logout Notification'; $message = 'User ' . $current_user->user_login . ' has logged out.'; wp_mail(get_option('admin_email'), $subject, $message); }
Display a custom message after logout
Show a custom message to the user after they have logged out.
add_action('wp_logout', 'display_logout_message'); function display_logout_message() { $_SESSION['logout_message'] = 'You have successfully logged out.'; }