By default, WordPress provides a standard logout link, which includes WP-nonce logout confirmation for added security. However, there are scenarios where website owners may wish to customize the process for a smoother user experience.
What's happening?
- When you create a logout link to;
https://domain.com/wp-login.php?action=logout
- But when your site members or users click the logout link or button, it asks for a confirmation that they wish to logout of the website. A bit Annoying, right?
What can we do?
It’s simple.
- Login to your FTP client or File manager in cPanel.
- Paste this snippet into your active theme’s functions.php and make sure to replace domain.com with your own domain on line 8.
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;
}
}
You may now construct your own user logout link and place it wherever you like! Example, in your menu, or anywhere in your page content as a hyperlink or a button.
One Response
After checking out a handful of the articles on your web page, I
seriously like your way of writing a blog. I added it to my bookmark webpage list and will be checking back in the near future.