How to add new fields to the “Add New User” screen in the wordpress admin panel?
I want to add a new field “Organization Name” to the add new user screen in wordpress backend.
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Aviance School is one of the largest web solutions platform in India for developers to learn and share their programming knowledge and build their careers.
I am using Arcane theme and I added organizations as post type. Also “vg_organization_id” was a user meta.
Please add the below code in functions.php file to get the extra field in user profile page.
function custom_user_profile_fields($user){
if(isset($_GET['user_id']) && $_GET['user_id'] != ""){
$user_id = $_GET['user_id'];
}else{
$user_id = "";
}
$organization = arcane_user_is_in_organization($user_id);
if($organization){
$get_org_data = get_post($organization);
$org_id = $get_org_data->ID;
}else{
$org_id = "";
}
if(is_object($user))
$orgs = arcane_get_organizations();
else
$orgs = arcane_get_organizations();
?>
<table class="form-table">
<tbody>
<tr class="form-field">
<th scope="row"><label for="vg-orgs">Select Organization</label></th>
<td>
<select name="vg_organization_id" id="vg_organization_id" >
<option value="">Select a organization</option>
<?php if($orgs !== false){ ?>
<?php foreach($orgs as $val){ ?>
<option value="<?php echo $val->ID; ?>" <?php if($org_id != "" && $org_id == $val->ID){echo "selected";} ?>><?php echo $val->post_title; ?></option>
<?php } ?>
<?php } ?>
</select>
</td>
</tr>
</tbody>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields', 30);
add_action( "user_new_form", "custom_user_profile_fields");
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if (isset($_REQUEST['vg_organization_id']) && $user_id != "") {
$org_id = $_REQUEST['vg_organization_id'];
update_user_meta($user_id, 'vg_organization_id', $org_id);
}
}
add_action('user_register','add_extra_profile_fields');
function add_extra_profile_fields($user_id){
if (isset($_REQUEST['vg_organization_id']) && $user_id != "") {
$org_id = $_REQUEST['vg_organization_id'];
add_user_meta($user_id, 'vg_organization_id', $org_id);
}
}