Quick guide for anyone trying to create their own custom log out links (from a menu or within their content).
So what’s the issue?
- You create a logout link to
https://domain.com/wp-login.php?action=logout
- But when you visit it…it asks for a confirmation. Annoying, right?
So to get past that we add a little snippet to functions.php (in your child theme).
The fix
Paste this snippet into your functions.php but replace yourdomain.com
. And now you can create your own users logout link anywhere you like!
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://yourdomain.com';
$location = str_replace('&', '&', wp_logout_url($redirect_to));
header("Location: $location");
die;
}
}
What about other fixes?
Long story short, I wrote this guide because the other fixes didn’t work. If you have other solutions to share, let me know in the comments.
ywl
I just accidentally found your website and it’s the most beautiful and well-designed one I have ever seen. Also thank you very much for writing such useful articles!I have saved the website to my bookmark and will come back regularly
Just a little thing to tell you(actually it’s why I leave a comment) that the email button to share this post turn white when hovered, the background is incorrectly set to #fff, this really bother me, so please fix it when you are not so busy
Hope you have a great day!
Julian
I had this issue before, noticed that a new plugging that I had just installed a few hours before, had the setting to block the /wp-login.php path lol
Sarah
This was EXACTLY what I was looking for!!! I have tried numerous other code snippets and this works perfectly. Thank you!
Xtof
Thanks Johnny
Same case as Sarah, tried numerous ones ans yours works just fine 🙂
Tin
Well, I’ll be damn!!! I too have been trying different snippets and this one finally worked. Thank you Johnny.
Esme
Thanks so much for sharing this clever tidbit!
Craig
Thank you so much for this snippet. It works a treat.
Protos
You saved us! We tried the same solution as you had with no luck, fortunately we found your site and it works flawlessly. Thank you so much!
abadi
Thank you so much.
Anton
Thank you so much
Matthias
I changed my login url with WPS Hide login. The snippet is not working because wp-login.php is not used anymore. Is there a modify possible to the snippet?
Kim
This code works great. Thank you. I needed something to use for a button URL. Again thanks.
Nesia
Thank you very much bro
Diffusioni Musicali
it does not work 🙁
https://diffusionimusicali.org/logout/?_wpnonce=fff32fe325
Mahesh Dubal
It works, Thank you
Randy Gallagher
I get a 418 using this.
Kea
Worked like a beautiful charm. Thank you
Chris
Wow! Thank you so much, this is exactly what I was looking for! It just works great!
Mark
Why is it that every time I find a script on your website, it just works!👍 I should probably just come here and search before using Google from now on. Usually I get Stackoverflow and it gets tiring trying script after script to no avail. Anyways, when google finally got me here, I recognized your name and website and had a feeling it was going to work. Keep up the good work! I wish I knew 1/100th of what you do.