简体   繁体   English

在Wordpress Workscout主题中注册后重定向到主页

[英]Redirect to Home page after registration in Wordpress Workscout theme

I am working on a wordpress job portal. 我正在一个wordpress工作门户上工作。 The problem that I am facing is that. 我面临的问题是。 The functionality of the registration and login form are; 注册和登录表单的功能如下: when a user registers, an email is generated to their registered email id with the password for their account.And the the registered user has to login using those credentials, but after every user registers for the site, the site directly logs the user in. I want the site to redirect to home or specific page after a user registers for the first time. 当用户注册时,将使用其帐户密码生成一封包含其密码的电子邮件到其注册电子邮件ID,并且注册用户必须使用这些凭据登录,但是在每个用户注册该站点之后,该站点都会直接将该用户登录。我希望网站在用户首次注册后重定向到首页或特定页面。

Registration Function 注册功能

      function workscout_registration_form() {

            // only show the registration form to non-logged-in members
            if(!is_user_logged_in()) {

                // check to make sure user registration is enabled
                $registration_enabled = get_option('users_can_register');

                // only show the registration form if allowed
                if($registration_enabled) { ?>
                    <form method="post" class="register workscout_form">

                        <?php do_action( 'woocommerce_register_form_start' ); ?>

                        <?php if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>

                            <p class="form-row form-row-wide">
                                <label for="reg_username"><?php _e( 'Username', 'workscout' ); ?> <span class="required">*</span>
                                <i class="ln ln-icon-Male"></i>
                                <input type="text" class="input-text" name="username" id="reg_username" value="<?php if ( ! empty( $_POST['username'] ) ) echo esc_attr( $_POST['username'] ); ?>" />
                                </label>
                            </p>
                        <?php endif; ?>

                        <p class="form-row form-row-wide">
                            <label for="reg_email"><?php _e( 'Email address', 'workscout' ); ?> <span class="required">*</span>
                            <i class="ln ln-icon-Mail"></i><input type="email" class="input-text" name="email" id="reg_email" value="<?php if ( ! empty( $_POST['email'] ) ) echo esc_attr( $_POST['email'] ); ?>" />
                            </label>
                        </p>

                        <?php if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>

                            <p class="form-row form-row-wide">
                                <label for="reg_password"><?php _e( 'Password', 'workscout' ); ?> <span class="required">*</span>
                                <i class="ln ln-icon-Lock-2"></i><input type="password" class="input-text" name="password" id="reg_password" />
                                </label>
                            </p>

                        <?php endif; ?>

                        <!-- Spam Trap -->
                        <div style="<?php echo ( ( is_rtl() ) ? 'right' : 'left' ); ?>: -999em; position: absolute;"><label for="trap"><?php _e( 'Anti-spam', 'workscout' ); ?></label><input type="text" name="email_2" id="trap" tabindex="-1" /></div>

                        <?php do_action( 'woocommerce_register_form' ); ?>
                        <?php do_action( 'register_form' ); ?>

                        <p class="form-row">
                            <?php wp_nonce_field( 'woocommerce-register' ); ?>
                            <input type="submit" class="button" name="register" value="<?php esc_attr_e( 'Register', 'workscout' ); ?>" />
                        </p>

                        <?php do_action( 'woocommerce_register_form_end' ); ?>

                    </form>
                <?php } else {
                   _e('User registration is not enabled','workscout');
                }

            }
        }
        add_shortcode('workscout_register_form', 'workscout_registration_form');

I don't have much knowledge on php, I am little confused between the functions I have added here. 我对php的了解不多,我在这里添加的功能之间很少混淆。

     function wc_custom_user_redirect( $redirect, $user ) {
            // Get the first of all the roles assigned to the user
            $role = $user->roles[0];
            $dashboard = admin_url();
            $myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );

            if( $role == 'employer' ) {

                if(get_option( 'job_manager_job_dashboard_page_id')) { 
                    $redirect_to = get_permalink(get_option( 'job_manager_job_dashboard_page_id')); 
                } else {
                    $redirect_to= home_url();
                };
            } elseif ( $role == 'candidate' ) {
                if(get_option( 'resume_manager_candidate_dashboard_page_id')) { 
                    $redirect_to = get_permalink(get_option( 'resume_manager_candidate_dashboard_page_id')); 
                } else {
                    $redirect_to= home_url();
                };
            } elseif ( $role == 'customer' || $role == 'subscriber' ) {
                //Redirect customers and subscribers to the "My Account" page
                $redirect = $myaccount;
            } else {
                //Redirect any other role to the previous visited page or, if not available, to the home
                $redirect = wp_get_referer() ? wp_get_referer() : home_url();
            }
            return $redirect;
        }
        add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );

Do i have to added some redirect code in this function, I tried commenting and adding a redirection code, but it didn't work 我是否必须在此函数中添加一些重定向代码,我尝试注释并添加重定向代码,但是没有用

    if ( ! function_exists( 'workscout_add_new_member' ) ) :
        function workscout_add_new_member() {

            $login_system = Kirki::get_option( 'workscout', 'pp_login_form_system' );

            if($login_system == 'workscout') {
                if (isset( $_POST["workscout_register_check"] ) && wp_verify_nonce($_POST['workscout_register_nonce'], 'workscout-register-nonce')) {

                    if ( !isset($_POST['confirm_email']) || $_POST['confirm_email'] !== '' ) {
                        home_url( '/' );
                        exit;
                    }
                    $user_login     = $_POST["workscout_user_login"];  
                    $user_email     = $_POST["workscout_user_email"];
                    $user_role      = $_POST["workscout_user_role"];


                    if(username_exists($user_login)) {
                        // Username already registered
                        workscout_form_errors()->add('username_unavailable', __('Username already taken','workscout'));
                    }
                    if(!validate_username($user_login)) {
                        // invalid username
                        workscout_form_errors()->add('username_invalid', __('Invalid username','workscout'));
                    }
                    if($user_login == '') {
                        // empty username
                        workscout_form_errors()->add('username_empty', __('Please enter a username','workscout'));
                    }
                    if(!is_email($user_email)) {
                        //invalid email
                        workscout_form_errors()->add('email_invalid', __('Invalid email','workscout'));
                    }
                    if(email_exists($user_email)) {
                        //Email address already registered
                        workscout_form_errors()->add('email_used', __('Email already registered','workscout'));
                    }

                    $password = wp_generate_password();
                    $password_generated = true;

                    if(empty($user_role)) {
                        $user_role = 'candidate';
                    }

                    $errors = workscout_form_errors()->get_error_messages();

                    // only create the user in if there are no errors
                    if(empty($errors)) {

                        $new_user_id = wp_insert_user(array(
                                'user_login'        => $user_login,
                                'user_pass'         => $password,
                                'user_email'        => $user_email,
                                'user_registered'   => date('Y-m-d H:i:s'),
                                'role'              => $user_role,
                            )
                        );
                        if($new_user_id) {
                            // send an email to the admin alerting them of the registration
                            // 
                            if (function_exists('sb_we_init')) {
                                wp_new_user_notification( $new_user_id, null, 'both' );
                            } else {
                                workscout_wp_new_user_notification($new_user_id,$password);
                            }

                            // log the new user in
                            // $creds = array();
                            // $creds['user_login'] = $user_login;
                            // $creds['user_password'] = $password;
                            // $creds['remember'] = true;

                            // $user = wp_signon( $creds, false );
                            // send the newly created user to the home page after logging them in
                            if ( is_wp_error($user) ){
                                echo $user->get_error_message();
                            } else {
                                wp_safe_redirect(  add_query_arg( 'success', 1,  home_url( '/' ) )  );
                            }

                            exit;
                        }

                    }

                }
            }
        }
        endif;
        add_action('init', 'workscout_add_new_member');

I found this additional code. 我找到了此附加代码。 Do you think home url should be added here ? 您是否认为应在此处添加家庭网址?

 function workscout_ajax_register(){

check_ajax_referer( 'ajax-register-nonce', 'security' );

// Nonce is checked, get the POST data and sign user on
$info = array();
$info['user_login'] = sanitize_user($_POST['username']);
$info['user_email'] = sanitize_email( $_POST['email']);
$info['user_role'] = sanitize_email( $_POST['role']);
$valid = true;
if(username_exists( $info['user_login'])) {
    // Username already registered
     echo json_encode(array('loggedin'=>false, 'message'=>__('Username already taken','workscout'), 'type'=>"error notification " ));
     $valid = false;
    die();
}
if(!validate_username( $info['user_login'])) {
    // invalid username
    echo json_encode(array('loggedin'=>false, 'message'=>__('Invalid username','workscout'), 'type'=>"error notification " ));
    $valid = false;
    die();
}
if( $info['user_login'] == '') {
    // empty username
    echo json_encode(array('loggedin'=>false, 'message'=>__('Please enter a username','workscout'), 'type'=>"error notification " ));
    $valid = false;
    die();
}
if(!is_email($info['user_email'])) {
    //invalid email
    echo json_encode(array('loggedin'=>false, 'message'=>__('Invalid email','workscout'), 'type'=>"error notification " ));
    $valid = false;
    die();
}
if(email_exists($info['user_email'])) {
    //Email address already registered
    echo json_encode(array('loggedin'=>false, 'message'=>__('Email already registered','workscout'), 'type'=>"error notification " ));
    $valid = false;
    die();
}

$password = wp_generate_password();
$password_generated = true;
$info['user_pass'] = $password;
// Register the user
if($valid) {
    $user_register = wp_insert_user( $info );
    if ( is_wp_error($user_register) ){ 
        $error  = $user_register->get_error_codes() ;

        if(in_array('empty_user_login', $error))
            echo json_encode(array('loggedin'=>false, 'message'=>__($user_register->get_error_message('empty_user_login')), 'type'=>"error notification " ));
        elseif(in_array('existing_user_login',$error))
            echo json_encode(array('loggedin'=>false, 'message'=>__('This username is already registered.','workscout'), 'type'=>"error notification " ));
        elseif(in_array('existing_user_email',$error))
            echo json_encode(array('loggedin'=>false, 'message'=> __('This email address is already registered.','workscout'), 'type'=>"error notification " ));

    } else {
         if (function_exists('sb_we_init')) {
            wp_new_user_notification( $user_register, null, 'both' );
        } else {
            workscout_wp_new_user_notification($user_register,$password);
        }
        $creds = array();
        $creds['user_login'] = $info['user_login'];
        $creds['user_password'] = $password;
        $creds['remember'] = true;

        $user_signon = wp_signon( $creds, false );
         if ( is_wp_error($user_signon) ){
            echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.','workscout'), 'type'=>"error notification " ));
        } else {
            wp_set_current_user($user_signon->ID); 
             $oUser = get_user_by( 'login', $creds['user_login'] );
            $aUser = get_object_vars( $oUser );
            $sRole = $aUser['roles'][0];
            echo json_encode(array('loggedin'=>true, 'message'=>__('Registration successful, redirecting...','workscout'), 'type'=>'success notification ', 'role'=> $sRole ));
        } 
    }
}

die();
 }
 endif;

 ?> 

Try this code 试试这个代码

add_filter( 'registration_redirect', 'my_redirect_home' );
function my_redirect_home( $registration_redirect ) {
    return home_url();
}

Please add code like below n check might be it works. 请添加如下所示的代码,n检查是否可行。

Please try it for both position by uncommenting. 请取消注释以尝试两个位置。

<form>
<p class="form-row">
<?php wp_nonce_field( 'woocommerce-register' ); ?>
<input type="submit" class="button" name="register" value="<?php esc_attr_e( 'Register', 'workscout' ); ?>" />
//<?php wp_redirect( home_url() );?>
</p>
<?php do_action( 'woocommerce_register_form_end' ); ?>
</form>
//<?php wp_redirect( home_url() ); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM