Logout Without Confirmation in WordPress

Hazrat Bilal

Software Engineer

WordPress Solutions tailored to your needs from anywhere in the world.

This code snippet allows users to log out of a WordPress site without the need for confirmation by bypassing the default logout nonce check. If the log-out action is triggered without a nonce, the script redirects the user to a specified URL after logging them out. This ensures a smoother logout experience, especially in scenarios where the nonce might not be included in the logout URL.

Add the following code to your theme file editor or Code Snippets plugin

add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
function logout_without_confirm($action, $result)
{
/**
* Allow logout without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : 'https://example.com/';
$location = str_replace('&', '&', wp_logout_url($redirect_to));
header("Location: $location");
die;
}
}

Explain Image

Any Question

Join To Get Updates

Related Snippets