最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

assign roles to users in custom drop down in signup form

programmeradmin1浏览0评论

I have made html signup form, in that i have added users role drop-down, while filling up signup form when i am selecting value from users role drop-down, its not getting updated in wp_usermeta table in database, by default value is subscriber.thanks in advance. I want to update database value from drop-down selection. I have 2 values in drop-down: user and photographer. Other values i have removed.

<?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() ?>

I have made html signup form, in that i have added users role drop-down, while filling up signup form when i am selecting value from users role drop-down, its not getting updated in wp_usermeta table in database, by default value is subscriber.thanks in advance. I want to update database value from drop-down selection. I have 2 values in drop-down: user and photographer. Other values i have removed.

<?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() ?>
Share Improve this question edited Nov 12, 2019 at 10:27 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Nov 12, 2019 at 8:03 priyapriya 151 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have to set user role using user id. use below code to assign role to user. add below code after this line :

    $user_id = wp_create_user($firstname,$lastname,$email);

set role to new created user. by default it will set subscriber role.

    $user_id_role = new WP_User($user_id);

    $user_role = !empty($_POST['role']) ? $_POST['role'] : 'subscriber';
    $user_id_role->set_role($user_role);

i have tested this code and it is working fine for me.let me know if this works for you!

发布评论

评论列表(0)

  1. 暂无评论