在注册表单的自定义下拉列表中为用户分配角色

时间:2019-11-12 作者:priya

我制作了html注册表单,因为我添加了“用户角色”下拉列表,当我在“用户角色”下拉列表中选择值时填写注册表单,它不会在数据库的wp\\u usermeta表中更新,默认值为subscriber。提前谢谢。我想从下拉选择中更新数据库值。下拉列表中有两个值:用户和摄影师。我已删除的其他值。

<?php  
/* 
Template Name: Register Page
*/

get_header() ?>
<?php
global $wpdb;

if($_POST){
     $firstname= $wpdb->escape($_POST[\'fname\']);
     $lastname= $wpdb->escape($_POST[\'lname\']);
    $email= $wpdb->escape($_POST[\'email\']);
     $confemail= $wpdb->escape($_POST[\'c_email\']);

     $error=array();

    if(empty($firstname)){
         $error[\'firstname_empty\']="username needed";
     }
     /*if(firstname_exists($firstname)){
     $error[\'firstname_exist\']="firstname already exists";

     }

     if(lastname_exists($lastname)){
     $error[\'lastname_exist\']="lastname already exists";

     }
*/

  if(!is_email($email)){
      $error[\'email_valid\']="enter valid email id";

     }

     if(email_exists($email)){
      $error[\'email_existence\']="email already exists";

     }

     if(strcmp($email, $confemail)){
      $error[\'email\']="email doesnt match";

     }


     if(count($error)==0) {

    $user_id = wp_create_user($firstname,$lastname,$email);
      echo "you have registered succesfully.. "; 
     $user_info = get_userdata($user_id);
    // create md5 code to verify later
    $code = md5(time());
    // make it into a code to send it to user via email
    $string = array(\'id\'=>$user_id, \'code\'=>$code);
    // create the activation code and activation status
    update_user_meta($user_id, \'account_activated\', 0);
    update_user_meta($user_id, \'activation_code\', $code);
    // create the url
    $url = get_site_url(). \'/email-verfication/?act=\' .base64_encode( serialize($string));
    // basically we will edit here to make this nicer
    $html = \'Please click the following link <br/><a href="\'.$url.\'">\'.$url.\'</a>\';
    echo "$html";

    wp_mail( $user_info->user_email, __(\'Email Subject\',\'text-domain\') , $html);
}
// send an email out to user
     exit();
     }
else{
        print_r($error);
        }
?>

<form name="signup-declaration" method="POST" id="signatory-sign-up-form">
    <div class="row">
      <div class="col-sm-6">
        <input type="text" name="fname" class="form-control" placeholder="First Name *">
      </div>
      <div class="col-sm-6">
        <input type="text" name="lname" class="form-control" placeholder="Last Name *">
      </div>
    </div><!--row ends-->
    <div class="row">
          <div class="col-sm-6">
        <input id="email" type="email" name="email" class="form-control" placeholder="Email *">
      </div>
      <div class="col-sm-6">
            <input type="email" name="c_email" class="form-control" placeholder="Confirm Email *">
      </div>
<?php global $wp_roles; ?>
Roles : <select name="role">
<?php foreach ( $wp_roles->roles as $key=>$value ): ?>
<option value="<?php echo $key; ?>"><?php echo $value[\'name\']; ?></option>
<?php endforeach; ?>
</select>
</select>

 <br/>
<br/>
      <div class="col-sm-6">
    <input type="submit" name="new_user_submit" value="sign-up">
    </div><!--row ends-->
  </div>
</form>


<?php get_footer() ?>

1 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

您必须使用用户id设置用户角色。使用以下代码将角色分配给用户。在此行后添加以下代码:

    $user_id = wp_create_user($firstname,$lastname,$email);
将角色设置为新创建的用户。默认情况下,它将设置订阅服务器角色。

    $user_id_role = new WP_User($user_id);

    $user_role = !empty($_POST[\'role\']) ? $_POST[\'role\'] : \'subscriber\';
    $user_id_role->set_role($user_role);
我已经测试了这段代码,它对我来说运行良好。如果这对你有用,请告诉我!

相关推荐