2017-05-08 131 views
0

我正在使用wordpress求职门户网站。我面临的问题是这样的。注册和登录表单的功能是;当用户注册时,会为其注册的电子邮件ID生成一个电子邮件,并为其帐户提供密码。注册用户必须使用这些凭据进行登录,但每个用户在为该网站注册后,都会直接登录该用户。我希望网站在用户第一次注册后重定向到家庭或特定页面。在Wordpress Workscout主题中注册后重定向到主页

注册功能

 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'); 

我没有对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); 

我是不是要在这个函数中添加了一些重定向代码,我想评论和添加重定向代码,但没有奏效

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'); 

我发现这个额外的代码。你认为应该在这里添加家庭网址吗?

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; 

?> 

回答

0

试试这个代码

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

究竟应该在哪里添加代码? – Dannyboi

+0

理想情况下,它应该在functions.php –

+0

嘿我在function.php中添加了代码,但它不起作用 – Dannyboi

0

请添加类似下面N检查可能是它的工作原理代码。

请通过取消注释来尝试这两个位置。

<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()); ?> 
+0

嘿@vipin,我试过了你的两个代码,仍然没有工作。 – Dannyboi

+0

嘿@vidish,我也试过你,还没有工作。 – Dannyboi