Categories wordpress Tutorials

Add Fields in edit profile page on wp admin

//We add action to hook “show_user_profile” and create three labels and input field in callback function and add action for same callback function
add_action( ‘show_user_profile’, ‘extra_user_profile_fields’ );
add_action( ‘edit_user_profile’, ‘extra_user_profile_fields’ );
function extra_user_profile_fields( $user ) { ?>
<!– HTML Start Section of Custome Fields –>
    <h3><?php _e(“Extra profile information”, “blank”); ?></h3>
    <table class=”form-table”>
    <tr>
        <th><label for=”dob”><?php _e(“DOB”); ?></label></th>
        <td>
            <input type=”text” name=”dob” id=”dob” value=”<?php echo esc_attr( get_the_author_meta( ‘dob’, $user->ID ) ); ?>” class=”regular-text” /><br />
            <span class=”description”><?php _e(“Please enter your date of birth.”); ?></span>
        </td>
    </tr>
    <tr>
        <th><label for=”statusCode”><?php _e(“Status Code”); ?></label></th>
        <td>
            <input type=”text” name=”statusCode” id=”statusCode” value=”<?php echo esc_attr( get_the_author_meta( ‘statusCode’, $user->ID ) ); ?>” class=”regular-text” /><br />
            <span class=”description”><?php _e(“Please enter your status code.”); ?></span>
        </td>
    </tr>
    <tr>
    <th><label for=”mobileCell”><?php _e(“Mobile/Cell”); ?></label></th>
        <td>
            <input type=”text” name=”mobileCell” id=”mobileCell” value=”<?php echo esc_attr( get_the_author_meta( ‘mobileCell’, $user->ID ) ); ?>” class=”regular-text” /><br />
            <span class=”description”><?php _e(“Please enter your Mobile or Cell No.”); ?></span>
        </td>
    </tr>
    <tr>
    <th><label for=”userstatus”><?php _e(“Status”); ?></label></th>
        <td>
            <input type=”text” name=”userstatus” id=”userstatus” value=”<?php echo esc_attr( get_the_author_meta( ‘userstatus’, $user->ID ) ); ?>” class=”regular-text” /><br />
            <span class=”description”><?php _e(“Please enter your Status.”); ?></span>
        </td>
    </tr>
    </table>
<!– End HTML Section of Custome Fields –>
<?php }
add_action( ‘personal_options_update’, ‘save_extra_user_profile_fields’ );
//to save the data for created custom fields we will use  “edit_user_profile_update” hook.
add_action( ‘edit_user_profile_update’, ‘save_extra_user_profile_fields’ );
function save_extra_user_profile_fields( $user_id ) {
// Update User Meta into the wp-usermeta table
update_user_meta( $user_id, ‘dob’, $_POST[‘dob’] );
    update_user_meta( $user_id, ‘statusCode’, $_POST[‘statusCode’] );
    update_user_meta( $user_id, ‘mobileCell’, $_POST[‘mobileCell’] );
    update_user_meta( $user_id, ‘userstatus’, $_POST[‘userstatus’] );
    // Get User data using user id like as $user_id
$user_info = get_userdata($user_id);
// Explode the whole email into array i.e test@gmail.com
// newemail and newpostemail variable’s contains two element into the $newemail array like as [0] => test@ and [1] =>gmail.com
$newemail = explode(“@”,$user_info->user_email);
$newpostemail = explode(“@”,$_POST[’email’]);
// Check if changed(new) email does’t equal to previous email and(&&) $newpostemail[1] does’t equal to “mymail.com” then go to if condition otherwise goto outside the if condition
if(($_POST[’email’] != $user_info->user_email) && ($newpostemail[1] != “mymail.com”)){
// $user_data contains the whole data of user which have $user_id i.e login user id
   $user_data = get_user_by( ‘id’, $user_id );
   // Get the values for wp-usermeta table like as nicknam,first_name,last_name etc.
$nickname = get_user_meta( $user_id , ‘nickname’, true );
  $first_name = get_user_meta( $user_id->ID , ‘first_name’, true );
  $last_name = get_user_meta( $user_id->ID , ‘last_name’, true );
  $nickname = get_user_meta( $user_id->ID , ‘nickname’, true );
  // Get the data object of an array
   $user_main_data = $user_data->data;
  // Assign the values
   $userLogin = $user_main_data->user_login;
   $userPass = $user_main_data->user_pass;
   $userNick = $user_main_data->user_nicename;
   $userEmail = $user_main_data->user_email;
   $userRegDt = $user_main_data->user_registered;
   $userDispNme = $user_main_data->display_name;
   // Get and assign password value to $password variable
   $password = “”;
   if($_POST[‘pass1-text’] != “”){
   // Assign the Changed passwrod to $password variable
   $password = $_POST[‘pass1-text’];
   }else{
   // Assign the Old password to $password variable
   $password = $user_info->user_login ;
   }
   //________________ Set new user data from previous user data______________//
   // Assign the Old user data to $userData variable as array form
   $userData = array(‘user_login’ => $userLogin ,
   ‘user_pass’ => $userPass,
   ‘user_nicename’ => $userNick,
   ‘user_email’=> $_POST[’email’],
   ‘user_status’ => 0,
   ‘user_pass’ => $password,
   ‘display_name’=> $userDispNme
);
   // Delete the old user form database
   wp_delete_user( $user_id );
   // Insert new user into database which contains old user data.
   $user_ids = wp_insert_user($userData);
   // Get the role of user
   $urole = $user_data->roles;
   // initialized the variable $oldRole
   $oldRole = “”;
   // Check if array fist value is not unregistered then assign that value to $oldRole otherwise second value is assigned
   if($urole[0] != “unregistered”){
   $oldRole = $urole[0];
   }else{
   $oldRole = $urole[1];
   }
   // Get the login user id
   $u = new WP_User( $user_ids );
   // Check if $oldRole variable is not empty then set role to that user othewise set as editor
   if($oldRole != “”){
   $u->set_role( $oldRole );
   }else{
   $u->set_role( ‘editor’ );
   }
//________________ End Set new user data from previous user data______________//
// Save User meta data from the new change values into wp-usermeta table
    update_user_meta( $user_ids, ‘nickname’, $_POST[‘nickname’] );
   update_user_meta( $user_ids, ‘first_name’, $_POST[‘first_name’] );
   update_user_meta( $user_ids, ‘last_name’, $_POST[‘last_name’] );
   update_user_meta( $user_ids, ‘dob’, $_POST[‘dob’] );
   update_user_meta( $user_ids, ‘statusCode’, $_POST[‘statusCode’] );
   update_user_meta( $user_ids, ‘mobileCell’, $_POST[‘mobileCell’] );
   update_user_meta( $user_ids, ‘userstatus’, $_POST[‘userstatus’] );
   //Redirect to the new create user edit page by using new created user id
   $url =”user-edit.php?user_id=”.$user_ids.”&wp_http_referer=%2Fidesignwordpress%2Fwp-admin%2Fusers.php”;
   wp_redirect( $url );
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *