• Skip to primary navigation
  • Skip to main content
  • Skip to footer

WPJohnny

WordPress Performance Guides and Reviews

  • Start a WordPress site
  • Hosting
  • Themes
  • Plugins
  • Blogging
  • Marketing

Custom LOG OUT link without WP-nonce confirmation

WordPress themes Dec 8, 2021 by Johnny 8 Comments

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.

Share this post:

Share on FacebookShare on TwitterShare on LinkedInShare on WhatsAppShare on EmailShare on SMS

Read all my posts on WordPress themes

About Johnny

Right on the edge of WordPress development! 10+ years of WordPress design, development, hosting, speed optimization, product advisor, marketing, monetization. I do all that.

More WordPress Guides

How much should good IM coaching cost?

Selling Ebooks vs Videos (the better info-product moneymaker?)

Enable GZIP for faster page loads!

WebP image format in 2020 – is it global yet?

WPCloudDeploy custom hacks and fixes

EASY Gutenberg Development Resources

Reader Interactions

8 Comments

  1. ywl

    February 25, 2022 at 9:44 am

    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!

    Reply
  2. Julian

    March 19, 2022 at 6:34 am

    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

    Reply
  3. Sarah

    July 2, 2022 at 6:13 pm

    This was EXACTLY what I was looking for!!! I have tried numerous other code snippets and this works perfectly. Thank you!

    Reply
  4. Xtof

    September 9, 2022 at 1:18 am

    Thanks Johnny
    Same case as Sarah, tried numerous ones ans yours works just fine 🙂

    Reply
  5. Tin

    September 27, 2022 at 7:25 pm

    Well, I’ll be damn!!! I too have been trying different snippets and this one finally worked. Thank you Johnny.

    Reply
  6. Esme

    November 5, 2022 at 9:37 am

    Thanks so much for sharing this clever tidbit!

    Reply
  7. Craig

    November 10, 2022 at 4:19 am

    Thank you so much for this snippet. It works a treat.

    Reply
  8. Protos

    February 3, 2023 at 4:57 pm

    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!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Footer

More links

  • Gadget reviews
  • Try my free WPJ plugins
  • Join the WPJ FB Group
  • WPJ YouTube & newsletter
  • Become a WPJ Affiliate

Popular Reviews

  • Best WordPress Hosting
  • Best WordPress Themes
  • Best WordPress Plugins
  • Best WordPress Cache Plugins

Services

  • Speed optimization
  • Speed optimization courses
  • WordPress hosting
  • Hire me or other experts
  • Client login

About Johnny

10+ years of WordPress design, development, hosting, speed optimization, marketing.
Contact me.

newsletter block

Copyright 2023 | WordPress guides by Johnny Nguyen

Click to Copy