Skip to content

Instantly share code, notes, and snippets.

@agusmu
Created January 26, 2015 00:11
Show Gist options
  • Save agusmu/0bbd0b0f60b2d7644f7d to your computer and use it in GitHub Desktop.
Save agusmu/0bbd0b0f60b2d7644f7d to your computer and use it in GitHub Desktop.
WordPress - Force Logout and Redirect After Logout
function amu_force_logout() {
$timeout = 300; // 5 minutes
if ( !is_user_logged_in() )
return;
global $user_ID;
$last_login = get_user_meta($user_ID, 'amu_login', true);
if ( time() > ( $last_login + $timeout ) ) {
wp_logout();
}
}
add_action( 'wp_head', 'amu_force_logout' );
add_action( 'admin_head', 'amu_force_logout' );
function amu_redirect_after_logout() {
$redirect = home_url('/'); // redirect url
wp_redirect( $redirect );
exit();
}
add_action( 'wp_logout', 'amu_redirect_after_logout' );
function amu_save_last_login( $login, $user ) {
update_user_meta( $user->ID, 'amu_login', time() );
}
add_action( 'wp_login', 'amu_save_last_login', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment