Home > database >  How to log out of WooCommerce without confirming?
How to log out of WooCommerce without confirming?

Time:02-05

We want the users of a website to close the session without having to confirm it.

Currently, when we press the "Log Out" button, the user is directed to the URL htt://myWebSite.com/my-account/customer-logout/ and we must press "Confirm and Exit"

Well, we want to avoid this step and automatically log out when the user clicks the Logout Button

There is some plugin that could do this, but we prefer to use functions. We've tried things, but we can't get it right to get it done

add_action('wp_logout','ps_redirect_after_logout');
function ps_redirect_after_logout(){
         wp_redirect( 'https://miPaginaFavorita.com' );
         exit();
}

Another option that we have handled is the following, but it does not work either

add_action('check_admin_referer', 'logout_without_confirm', 10, 2);

   function logout_without_confirm($action, $result)

      {

      /**

      * Allow log out without confirmation

      */

      if ($action == "log-out" && !isset($_GET['_wpnonce'])) {

      $redirect_to = isset($_REQUEST['redirect_to']) ?
 function logout_without_confirm($action, $result)

      {

      /**

      * Allow log out without confirmation

      */

      if ($action == "log-out" && !isset($_GET['_wpnonce'])) {

      $redirect_to = isset($_REQUEST['redirect_to']) ?

      $_REQUEST['redirect_to'] : '';

      $location = str_replace('&', '&', wp_logout_url($redirect_to));;

      header("Location: $location");

      die();

    }}

How can I modify my function to avoid confirming when logging out? Thanks

CodePudding user response:

you most change url function in your button . check your theme file how create button for logout . use 'wp_logout_url' function for skip Confirm page . in my theme like this :

if ( class_exists( 'WooCommerce' ) ) {
    $logout_link = wc_get_endpoint_url( 'customer-logout', '', wc_get_page_permalink( 'myaccount' ) );
}
$logout_btn = '<a  href="' . esc_url( $logout_link ) . '">Logout</a>';   

For example, I did this to skip the page confirmation and work fine :

$logout_link = wp_logout_url( get_home_url() );
$logout_btn = '<a  href="' . esc_url( $logout_link ) . '">Logout</a>';

I hope it helped you.

Edit --

METHOD 2 :

simple way => if you are redirect to "/customer-logout" when click logout button you can check and logout user with this function . add code in your functions.php (is in your theme folder or child theme folder) .

function skip_logout_confirmation() {
global $wp;
if ( isset( $wp->query_vars['customer-logout'] ) ) {
    wp_redirect( str_replace( '&amp;', '&', wp_logout_url( home_url() ) ) );
    exit;
  }
}
add_action( 'template_redirect', 'skip_logout_confirmation' );
  • Related