简体   繁体   English

用户在 WordPress 上注册时更改用户角色

[英]Change user role when user registers on WordPress

I'm using the User Registration plugin by WPEverest to build my registration form.我正在使用WPEverest 的用户注册插件来构建我的注册表单。 My form has a select option for the user to pick their user role - Applicant or Provider.我的表单有一个 select 选项供用户选择他们的用户角色 - 申请人或提供者。 Below is select input.下面是 select 输入。

<form method='post' class='register' data-form-id="57"
              data-enable-strength-password="" data-minimum-password-strength="3"  data-captcha-enabled="">
..
...
    <select data-rules="" data-id="role" name="role" id="role" class="select ur-frontend-field  " required="required" data-label="What is Your Role?" data-allow_clear="true" data-placeholder="">
        <option value="Applicant"  selected='selected'>Applicant</option>
        <option value="Provider" >Provider</option>
    </select>
...
...
</form> 

Here's my hook in functions.php but somehow it doesn't save the user as the role Provider but only as the default role - Applicant.这是我在functions.php中的钩子,但不知何故它不会将用户保存为角色提供者,而只是作为默认角色 - 申请人。

add_action('user_register', 'update_role');
function update_role($user_id, $meta=array() ) {

   if ( isset( $_POST['role'] ) ) {
       $userdata = array();
       $userdata['ID'] = $user_id;
       $role = htmlspecialchars($_POST['role'], ENT_QUOTES, 'UTF-8');
      
       //Only allow if user role is those we recognize
       if ( $role == 'Applicant' ) {
           $userdata['role'] = 'applicant';
           wp_update_user($userdata);
           
       } else if ( $role == 'Provider') {
            $userdata['role'] = 'provider';
            wp_update_user($userdata);
       }
   }
}

I tested that $_POST['role'] also returns nothing.我测试了$_POST['role']也没有返回任何内容。 Anyone knows how to solve this?任何人都知道如何解决这个问题?

I figured it out.我想到了。 I need to call the plugin's hook as below, not the user_register .我需要如下调用插件的钩子,而不是user_register It's fired under wp-content\plugins\user-registration\includes\frontend\class-ur-frontend-form-handler.php它在wp-content\plugins\user-registration\includes\frontend\class-ur-frontend-form-handler.php下触发

do_action( 'user_registration_after_register_user_action', self::$valid_form_data, $form_id, $user_id );

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

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