1

How can I add an input field to the admin section Users -> Add New?

I want to add a field for a phone number, but don't want a plugin to do that.

3

Here's the way I have always done it. All of this code goes in functions.php. Of course this adds a whole new section to the user profile.

add_action( 'show_user_profile', 'be_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'be_show_extra_profile_fields' );

function be_show_extra_profile_fields( $user ) { ?>

    <h3>Extra Contact Information</h3>

    <table class="form-table">
        <tr>
            <th><label for="contact">Contact Number</label></th>
            <td>
                <input type="text" name="contact" id="contact" value="<?php echo esc_attr( get_the_author_meta( 'contact', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Please enter your phone number.</span>
            </td>
        </tr>
    </table>
<?php } 
add_action( 'personal_options_update', 'be_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'be_save_extra_profile_fields' );

function be_save_extra_profile_fields( $user_id ) {

    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }

    update_usermeta( $user_id, 'contact', esc_attr( $_POST['contact'] ) );
}

Alternatively If you want to add some contact methods to the already existing section in the user profile you can use the following and it's a little cleaner. Below is the function i usually use to get rid of outdated social platforms and add some new ones for users.

add_filter( 'user_contactmethods', 'be_hide_profile_fields', 10, 1 );
function be_hide_profile_fields( $contactmethods ) {
    unset($contactmethods['aim']);
    unset($contactmethods['jabber']);
    unset($contactmethods['yim']);

    $contactmethods['twitter'] = 'Twitter';
    $contactmethods['facebook'] = 'Facebook';
    $contactmethods['linkedin'] = 'LinkedIn';
    return $contactmethods;
}

If you wanted to add a phone number to that you could just add something like

$contactmethods['phonenumber'] = 'Phone Number;

to the function above and it will create the field for you.

  • use update_user_meta instead. update_usermeta is going to deprecated. – Dashrath Nov 7 at 11:49
2

Okay found the answer by poking around the code

A field can be added by doing the following:

add_action('user_new_form','myplugin_add_field');

function myplugin_add_field() {
    // code to add field
}
0

in http://plugins.svn.wordpress.org/hrm-work-tracking/trunk/hrm-users.php I found such thing with

add_action( 'show_user_profile', '' );
add_action( 'edit_user_profile', '' );
add_action( 'personal_options_update', '' );
add_action( 'edit_user_profile_update', '' );

I dont know another way for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.