Are you seeing the confirmation “Do you really want to logout?” after clicking the logout button in WordPress and do you want to skip this? if yes, then you are at the right place. In this article, we will see how to log out without confirmation in WordPress. Let’s see how to do it.
01 Logout Without Confirmation Message In WordPress Using Action Hook
add_action('check_admin_referer', 'scratchcode_logout_without_confirm', 10, 2);
function scratchcode_logout_without_confirm($action, $result){
/**
* Allow logout without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])):
$redirectUrl = 'your redirect url';
wp_redirect( str_replace( '&', '&', wp_logout_url( $redirectUrl.'?logout=true' ) ) );
exit;
endif;
}
Note: Don’t forget to replace your redirect URL on line 7 on the above example.
Explanation:
In the above example, we have added the check_admin_referer action hook. This hook is responsible for checking security nonce for each action performed in WordPress.
So if this hook doesn’t found any valid security nonce then it will shows the “Are you sure?” type message.
So basically, we are by passing this check for only logout action in the above example.
02 Use wp_logout_url() Function
Most beginners make mistakes while coding in WordPress due to a lack of knowledge. So while developing logout link using different functions instead use wp_logout_url() function.
<a href="<?php echo wp_logout_url(); ?>">Logout</a>
The above code will retrieve the logout URL with necessary nonce.
<a href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>
In the above code will logout and redirect to the homepage.
<a href="<?php echo wp_logout_url( get_permalink() ); ?>">Logout</a>
Above code will logout and redirect to the current page.
That’s it for now. We hope this article helped you to learn how to logout without confirmation in WordPress.
Additionally, read our guide:
- How to Add Products Per Page Dropdown in WooCommerce
- “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
- How to Create a Plugin in WordPress from Scratch
- How to Disable Admin Bar in WordPress Without Plugin
- How To Send Custom Emails in WordPress
- How to Allow Preview of Draft Post Without Login in WordPress
- Import Users From CSV In WordPress Programmatically
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!
i love this excellent post
Hey, Augustus Strople
I am glad that you like the post. Thank you 🙂
Excellent article. Keep writing such kind of info on your blog.
Im really impressed by your blog.
Hi there, You’ve done a fantastic job. I will definitely digg it and personally recommend
to my friends. I am confident they will be benefited from this site.
We are glad to have you here. Thanks
I used to be suggested this blog through my cousin. I’m no longer positive whether this post is written by him as no
one else realize such particular approximately
my problem. You are incredible! Thank you!