Home » How To Logout Without Confirmation In WordPress

How To Logout Without Confirmation In WordPress

Last updated on December 23, 2020 by

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:

  1. How to Add Products Per Page Dropdown in WooCommerce
  2. “Sorry, your session has expired. Return to homepage” – WordPress WooCommerce Error
  3. How to Create a Plugin in WordPress from Scratch
  4. How to Disable Admin Bar in WordPress Without Plugin
  5. How To Send Custom Emails in WordPress
  6. How to Allow Preview of Draft Post Without Login in WordPress
  7. 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!

 
 

2 thoughts on “How To Logout Without Confirmation In WordPress”

Leave a Comment

*

code