We already know that the ‘Are you sure you want to log out?’ error message is displayed since the link is missing nonce.
In that case, adding a nonce to the log-out link should solve the issue and log us out without the prompt.
Therefore, we should add a nonce field to every logout link on our website (specifically to those we created ourselves).
Copy and paste the code below into the functions.php file of your child theme file or a dedicated 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://domain.com';
$location = str_replace('&', '&', wp_logout_url($redirect_to));
header("Location: $location");
die;
}
}
The simple code snippet above will add a nonce field value to every logout link that’s missing it.
The added nonce field will prevent visitors from seeing the ‘Are you sure?’ message when they follow your Log Out link.
You Are Attempting To Log Out – Frequently Asked Questions
WordPress adds a security nonce to the logout URL to prevent Cross-Site Request Forgery (CSRF) attacks. If you click a logout link without a valid nonce, WordPress asks for confirmation to ensure the request was intentional.
You can modify the logout URL to include a valid nonce, which skips the confirmation step.
If the logout link already contains a valid nonce, WordPress will log you out immediately without asking for confirmation.
The message is built into WordPress and cannot be easily changed without modifying core files. However, you can create a custom logout page by redirecting users to a custom page instead of showing the default confirmation message.