How to log out without confirmation ‘Do you really want to log out?”?

Right now when I log out via:

<a href="<?php bloginfo('url'); ?>/wp-login.php?action=logout" rel="nofollow noreferrer noopener">Log out</a>

it redirects me to the page where I need to confirm the log out.

How to eliminate the confirmation and redirect to the homepage after logout?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This happens because you are missing the neccessary nonce in the URL, which is being checked in wp-login.php

case 'logout' :
    check_admin_referer('log-out');
    ...

Use wp_logout_url in order to retreive the URL including the nonce. If you want to redirect to a custom URL, simply pass it as an argument.

<a href="<?php echo wp_logout_url('/redirect/url/goes/here') ?>" rel="nofollow noreferrer noopener">Log out</a>

You could also use wp_loginout which generates the link for you including translation:

echo wp_loginout('/redirect/url/goes/here')

Method 2

If you can’t use wp_logout_url() function, You can turn off this validation using this code:

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'] : 'url-you-want-to-redirect';
        $location = str_replace('&amp;', '&', wp_logout_url($redirect_to));
        header("Location: $location");
        die;
    }
}

Replace 'url-you-want-to-redirect' with the URL you want to redirect after logout.

Add it in your functions.php

Method 3

If you create a custom link in your menu, set the label to “Logout”, and set the URL to http://yourdomain.com/wp-login.php?action=logout. Then add this function to your functions.php file:

function change_menu($items){
  foreach($items as $item){
    if( $item->title == "Logout"){
         $item->url = $item->url . "&_wpnonce=" . wp_create_nonce( 'log-out' );
    }
  }
  return $items;

}
add_filter('wp_nav_menu_objects', 'change_menu');

If you want to redirect to the login page after logout then you should append login URL as:

$item->url = $item->url . "&_wpnonce=" . wp_create_nonce( 'log-out' ).'&redirect_to='.wp_login_url();

Reference link

** tried that did not work. Really want to log out page then 4 something went wrong when clicking the button.

Method 4

If you’re using roots/sage wordpress starter theme, or any other theme using Laravel’s Blade templating engine, make sure not to escape the url:

// don't do
<a href="{{ wp_logout_url(get_permalink()) }}">log out</a>

// but instead
<a href="{!! wp_logout_url(get_permalink()) !!}">log out</a>

First example escapes all html characters, while the latter does not.

Method 5

This worked for me by adding /?customer-logout=true at the end.

http://www.website.com/?customer-logout=true


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x