在woocommerce中注册并重定向到登录页面时阻止自动登录?

时间:2014-06-24 05:51:59

标签: php wordpress login woocommerce

我正在设计一个带有woo commerce wordpress的网站我通过引用this solution

分隔了登录和注册页面

如何在注册成功后将注册页面重定向到登录页面而无需登录。用户需要使用通过电子邮件发送的用户名和密码登录。

我的登录页面是

www.example.com/my-account /

和注册页面是

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

4 个答案:

答案 0 :(得分:11)

经过大量搜索,我找到了解决方案

第1步:添加WP Approve User

步骤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_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);

答案 1 :(得分:3)

以下代码行位于woocommerce/includes/class-wc-form-handler.php第905行。

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

我更正@ user3518270给出的答案

下面的行不起作用,因为它是woocommerce使用的过滤器所以需要使用add_filter()而不是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);

答案 2 :(得分:1)

您可以使用woocommerce过滤器来防止身份验证。

将此片段添加到子主题中的functions.php文件中:

add_filter( 'woocommerce_registration_auth_new_customer', '__return_false' );

答案 3 :(得分:0)

您可以使用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 }