Prevent automatic login when register in woocommer

2020-02-26 10:36发布

I am designing a website with woo commerce wordpress I have separate the login and register page by reference to this solution

How can I redirect the registration page to login page after successful registration without logged in. The user need to login there with the emailed username and password.

my login page is

www.example.com/my-account/

and registration page is

www.example.com/my-account/?action=register

4条回答
我只想做你的唯一
2楼-- · 2020-02-26 11:02

You can use the woocommerce filter to prevent authentication.

Add this snippet to you functions.php file in child theme:

add_filter( 'woocommerce_registration_auth_new_customer', '__return_false' );
查看更多
你好瞎i
3楼-- · 2020-02-26 11:10

Below line of code is located at woocommerce/includes/class-wc-form-handler.php line no 905.

wp_redirect( apply_filters( 'woocommerce_registration_redirect', $redirect ) );

I correcting answer given by @user3518270

Below line will not work as it is filter used by woocommerce So need to use add_filter() instead of add_action()

add_action('woocommerce_registration_redirect', 'user_autologout', 2);

/* Stop auto login */


function user_autologout(){
       if ( is_user_logged_in() ) {
                $current_user = wp_get_current_user();
                $user_id = $current_user->ID;
                $approved_status = get_user_meta($user_id, 'wp-approve-user', true);
                //if the user hasn't been approved yet by WP Approve User plugin, destroy the cookie to kill the session and log them out
        if ( $approved_status == 1 ){
            return $redirect_url;
        }
                else{
            wp_logout();
                        return get_permalink(woocommerce_get_page_id('myaccount')) . "?approved=false";
                }
        }
}
add_filter('woocommerce_registration_redirect', 'user_autologout', 2);

function registration_message(){
        $not_approved_message = '<p class="registration">Send in your registration application today!<br /> NOTE: Your account will be held for moderation and you will be unable to login until it is approved.</p>';
        if( isset($_REQUEST['approved']) ){
                $approved = $_REQUEST['approved'];
                if ($approved == 'false')  echo '<p class="registration successful">Registration successful! You will be notified upon approval of your account.</p>';
                else echo $not_approved_message;
        }
        else echo $not_approved_message;
}
add_action('woocommerce_before_customer_login_form', 'registration_message', 2);
查看更多
手持菜刀,她持情操
4楼-- · 2020-02-26 11:10

You can do this using cookie,

function.php

setcookie('afterRegister', null, -1, '/');
function user_autologout(){
   if ( is_user_logged_in() ) {
      wp_logout();
      setcookie("afterRegister", "afterRegister", time() + (86400 * 30), "/");
      return get_permalink(woocommerce_get_page_id('myaccount'));

   }
}
add_filter('woocommerce_registration_redirect', 'user_autologout', 2);

page-my-account.php

<?php if(isset($_COOKIE["afterRegister"]) && !empty($_COOKIE["afterRegister"]) ) {?>
<div class="woocommerce-message" role="alert">
Your account has been created successfully, Please login using the auto generated password we've sent on your email address</div>
<?php }
查看更多
你好瞎i
5楼-- · 2020-02-26 11:19

After a lot of search I found the solution for this

Step1: add WP Approve User

Step2: add these code to ur theme function file

/* Stop auto login */


function user_autologout(){
       if ( is_user_logged_in() ) {
                $current_user = wp_get_current_user();
                $user_id = $current_user->ID;
                $approved_status = get_user_meta($user_id, 'wp-approve-user', true);
                //if the user hasn't been approved yet by WP Approve User plugin, destroy the cookie to kill the session and log them out
        if ( $approved_status == 1 ){
            return $redirect_url;
        }
                else{
            wp_logout();
                        return get_permalink(woocommerce_get_page_id('myaccount')) . "?approved=false";
                }
        }
}
add_action('woocommerce_registration_redirect', 'user_autologout', 2);
function registration_message(){
        $not_approved_message = '<p class="registration">Send in your registration application today!<br /> NOTE: Your account will be held for moderation and you will be unable to login until it is approved.</p>';
        if( isset($_REQUEST['approved']) ){
                $approved = $_REQUEST['approved'];
                if ($approved == 'false')  echo '<p class="registration successful">Registration successful! You will be notified upon approval of your account.</p>';
                else echo $not_approved_message;
        }
        else echo $not_approved_message;
}
add_action('woocommerce_before_customer_login_form', 'registration_message', 2);
查看更多
登录 后发表回答