How To Add Text Box In User Profile

Posted By Renu Yadav | 30-Apr-2018

In WordPress have many build in functionality which makes our work easy. We don't need that much functionality to add from our side. Wordpress already has predefined functions according to our need.

 

Add below code in your function.php. After adding the code, login to admin panel and check the user profile. You can see now there is employee id text box. If you add the employee id in the text box then save the employee id. After that, you can see you can edit the employee id in the admin panel. We don't need to add the code for access the employee id of the user and don't need to write the code for saving the employee id in the database because WordPress have a predefined function to save the value of employee id and access the value of employee id in the text box.

 

Employee id value will store in wp_usermeta table . Employee id text will store in meta_key column  and value of employee id will store in meta_value column . These values are stored in a table according to the user id. In wp_usermeta table, there is a user_id column, where WordPress store the user id. You have user id column so that if you want to access employee id according to the user id.You can access the data. If you want to access the whole employee id of all user.You have the wp_usermeta table. You can easily access all employee id of all user.

 

// employee id text box in user profile

function custom_user_profile_fields_emp_id($user){
  ?>
    
    <table class="form-table">
        <tr>
            <th><label for="emp_id">Employee ID</label></th>
            <td>
                <input type="text" class="regular-text" name="emp_id" value="<?php echo esc_attr( get_the_author_meta( 'emp_id', $user->ID ) ); ?>" id="emp_id"/><br />
               <span class="description">Please enter employee id.</span> 
            </td>
        </tr>
    </table>
  <?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields_emp_id' );
add_action( 'edit_user_profile', 'custom_user_profile_fields_emp_id' );
add_action( "user_new_form", "custom_user_profile_fields_emp_id" );

function save_custom_user_profile_fields_emp_id($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    update_usermeta($user_id, 'emp_id', $_POST['emp_id']);
}
add_action('user_register', 'save_custom_user_profile_fields_emp_id');
add_action('profile_update', 'save_custom_user_profile_fields_emp_id');

Like above field, we can also make the company name, telephone number, city, pin code, father name, Adhar card, PAN card number Address, office address etc field in the user profile. You can add many fields in user profile page as you want. The text box is very useful for the user profile. He can add the details and also update the details.

Request for Proposal

Recaptcha is required.

Sending message..