Adding fields to the “Add New User” screen in the dashboard

I’d like to add the field “Company Name” to the add new user page in the admin panel. I’ve done quite a bit of searching and have been unable to find details on how to do this. I can easily add info to the profile page and registration with..

   function my_custom_userfields( $contactmethods ) {
    //Adds customer contact details
    $contactmethods['company_name'] = 'Company Name';
    return $contactmethods;
   }
   add_filter('user_contactmethods','my_custom_userfields',10,1);

But no dice on anything else.

Related posts

Leave a Reply

9 comments

  1. user_new_form is the hook that can do the magic here.

    function custom_user_profile_fields($user){
      ?>
        <h3>Extra profile information</h3>
        <table class="form-table">
            <tr>
                <th><label for="company">Company Name</label></th>
                <td>
                    <input type="text" class="regular-text" name="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" id="company" /><br />
                    <span class="description">Where are you?</span>
                </td>
            </tr>
        </table>
      <?php
    }
    add_action( 'show_user_profile', 'custom_user_profile_fields' );
    add_action( 'edit_user_profile', 'custom_user_profile_fields' );
    add_action( "user_new_form", "custom_user_profile_fields" );
    
    function save_custom_user_profile_fields($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, 'company', $_POST['company']);
    }
    add_action('user_register', 'save_custom_user_profile_fields');
    add_action('profile_update', 'save_custom_user_profile_fields');
    

    For more details visit my blog post: http://scriptbaker.com/adding-custom-fields-to-wordpress-user-profile-and-add-new-user-page/

  2. I had the same need and created the following hack:

    <?php
    function hack_add_custom_user_profile_fields(){
        global $pagenow;
    
        # do this only in page user-new.php
        if($pagenow !== 'user-new.php')
            return;
    
        # do this only if you can
        if(!current_user_can('manage_options'))
            return false;
    
    ?>
    <table id="table_my_custom_field" style="display:none;">
    <!-- My Custom Code { -->
        <tr>
            <th><label for="my_custom_field">My Custom Field</label></th>
            <td><input type="text" name="my_custom_field" id="my_custom_field" /></td>
        </tr>
    <!-- } -->
    </table>
    <script>
    jQuery(function($){
        //Move my HTML code below user's role
        $('#table_my_custom_field tr').insertAfter($('#role').parentsUntil('tr').parent());
    });
    </script>
    <?php
    }
    add_action('admin_footer_text', 'hack_add_custom_user_profile_fields');
    
    
    function save_custom_user_profile_fields($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, 'my_custom_field', $_POST['my_custom_field']);
    }
    add_action('user_register', 'save_custom_user_profile_fields');
    
  3. You need to do 2 things.

    1. Register fields
    2. Save fields

    Note: Below example works only for administrator user role.


    1. Register fields

    For Add New User use action user_new_form

    For User Profile use actions show_user_profile, edit_user_profile

    Register fields Snippet:

    /**
     * Add fields to user profile screen, add new user screen
     */
    if( !function_exists('m_register_profile_fields') ) {
    
        //  This action for 'Add New User' screen
        add_action( 'user_new_form', 'm_register_profile_fields' );
    
        //  This actions for 'User Profile' screen
        add_action( 'show_user_profile', 'm_register_profile_fields' );
        add_action( 'edit_user_profile', 'm_register_profile_fields' );
    
        function m_register_profile_fields( $user ) {
    
            if ( !current_user_can( 'administrator', $user_id ) )
                return false;
    
            ?>
    
            <h3>Client Portal</h3>
            <table class="form-table">
                <tr>
                    <th><label for="dropdown">Portal Category</label></th>
                    <td>
                        <input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( 'portal_cat', $user->ID ) ); ?>" id="portal_cat" /><br />
                    </td>
                </tr>
            </table>
        <?php }
    }
    

    2. Save fields

    For Add New User use action user_register

    For User Profile use actions personal_options_update, edit_user_profile_update

    Save fields Snippet:

    /**
     *  Save portal category field to user profile page, add new profile page etc
     */
    if( !function_exists('m_register_profile_fields') ) {
    
        //  This action for 'Add New User' screen
        add_action( 'user_register', 'cp_save_profile_fields' );
    
        //  This actions for 'User Profile' screen
        add_action( 'personal_options_update', 'cp_save_profile_fields' );
        add_action( 'edit_user_profile_update', 'cp_save_profile_fields' );
    
        function cp_save_profile_fields( $user_id ) {
    
            if ( !current_user_can( 'administrator', $user_id ) )
                return false;
    
            update_usermeta( $user_id, 'portal_cat', $_POST['portal_cat'] );
        }
    }
    

    Complete Code Snippet:

    /**
     * Add fields to user profile screen, add new user screen
     */
    if( !function_exists('m_register_profile_fields') ) {
    
        //  This action for 'Add New User' screen
        add_action( 'user_new_form', 'm_register_profile_fields' );
    
        //  This actions for 'User Profile' screen
        add_action( 'show_user_profile', 'm_register_profile_fields' );
        add_action( 'edit_user_profile', 'm_register_profile_fields' );
    
        function m_register_profile_fields( $user ) {
    
            if ( !current_user_can( 'administrator', $user_id ) )
                return false;
    
            ?>
    
            <h3>Client Portal</h3>
            <table class="form-table">
                <tr>
                    <th><label for="dropdown">Portal Category</label></th>
                    <td>
                        <input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( 'portal_cat', $user->ID ) ); ?>" id="portal_cat" /><br />
                    </td>
                </tr>
            </table>
        <?php }
    }
    
    
    /**
     *  Save portal category field to user profile page, add new profile page etc
     */
    if( !function_exists('m_register_profile_fields') ) {
    
        //  This action for 'Add New User' screen
        add_action( 'user_register', 'cp_save_profile_fields' );
    
        //  This actions for 'User Profile' screen
        add_action( 'personal_options_update', 'cp_save_profile_fields' );
        add_action( 'edit_user_profile_update', 'cp_save_profile_fields' );
    
        function cp_save_profile_fields( $user_id ) {
    
            if ( !current_user_can( 'administrator', $user_id ) )
                return false;
    
            update_usermeta( $user_id, 'portal_cat', $_POST['portal_cat'] );
        }
    }
    
  4. The hooks are important, no matter how we sorted form fields inside the function. Follow my inline comments. As of WordPress 4.2.2 we have plenty of hooks now:

    <?php
    /**
     * Declaring the form fields
     */
    function show_my_fields( $user ) {
       $fetched_field = get_user_meta( $user->ID, 'my_field', true ); ?>
        <tr class="form-field">
           <th scope="row"><label for="my-field"><?php _e('Field Name') ?> </label></th>
           <td><input name="my_field" type="text" id="my-field" value="<?php echo esc_attr($fetched_field); ?>" /></td>
        </tr>
    <?php
    }
    add_action( 'show_user_profile', 'show_my_fields' ); //show in my profile.php page
    add_action( 'edit_user_profile', 'show_my_fields' ); //show in my profile.php page
    
    //add_action( 'user_new_form_tag', 'show_my_fields' ); //to add the fields before the user-new.php form
    add_action( 'user_new_form', 'show_my_fields' ); //to add the fields after the user-new.php form
    
    /**
     * Saving my form fields
     */
    function save_my_form_fields( $user_id ) {
        update_user_meta( $user_id, 'my_field', $_POST['my_field'] );
    }
    add_action( 'personal_options_update', 'save_my_form_fields' ); //for profile page update
    add_action( 'edit_user_profile_update', 'save_my_form_fields' ); //for profile page update
    
    add_action( 'user_register', 'save_my_form_fields' ); //for user-new.php page new user addition
    
  5. user_contactmethods filter hook does not get called at the user-new.php page so that wont work and sadly if you take a look at the source you will see that there is no hook that can be used to add extra fields to the add new user form.

    So this can only be done by either modifying core files (BIG NO NO) or adding the fields using JavaScript or jQuery and catching the fields.

    or you can create a Ticket at the Trac

  6. I workaround is available by using the user_new_form_tag which resides inside the user-new.php page’s form starting tag. It’s in the end so if you output HTML after that you just need to begin the output with > and remove the last outputted > of your own code. As in:

    function add_new_field_to_useradd()
    {
        echo "><div>"; // Note the first '>' here. We wrap our own output to a 'div' element.
    
        // Your wanted output code should be here here.
    
        echo "</div"; // Note the missing '>' here.
    }
    
    add_action( "user_new_form_tag", "add_new_field_to_useradd" );
    

    The user_new_form_tag is situated in user-new.php around line 303 (in WP3.5.1 at least):

    ...
    <p><?php _e('Create a brand new user and add it to this site.'); ?></p>
    <form action="" method="post" name="createuser" id="createuser" class="validate"<?php do_action('user_new_form_tag');?>>
    <input name="action" type="hidden" value="createuser" />
    ...
    

    Of course the downside here is that all your custom field must appear first in the form, before the fields declared in WP core.

  7. The following code will display “Biographical Info” in “Add User” form

    
    function display_bio_field() {
      echo "The field html";
    }
    add_action('user_new_form', 'display_bio_field');
    
    
  8. In order to do this you’ll have to manually change the user-new.php page. It’s not the correct way to handle it but if you’re in desperate need this is how it’s done.

    I added

    <tr class="form-field">
        <th scope="row"><label for="company_name"><?php _e('Company Name') ?> </label></th>
        <td><input name="company_name" type="text" id="company_name" value="<?php echo esc_attr($new_user_companyname); ?>" /></td>
    </tr>
    

    I also added the information to functions.php

       function my_custom_userfields( $contactmethods ) {
        $contactmethods['company_name']             = 'Company Name';
        return $contactmethods;
       }
       add_filter('user_contactmethods','my_custom_userfields',10,1);
    
  9. This won’t do it for the add new user page, but if you want to make it happen in the “Your Profile” page (where users can edit their profile), then you can try this in functions.php:

    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    function my_show_extra_profile_fields( $user ) { ?>
        <h3>Extra profile information</h3>
        <table class="form-table">
            <tr>
                <th><label for="companyname">Company Name</label></th>
                <td>
                    <input type="text" name="companyname" id="companyname" value="<?php echo esc_attr( get_the_author_meta( 'companyname', $user->ID ) ); ?>" class="regular-text" /><br />
                    <span class="description">Where are you?</span>
                </td>
            </tr>
        </table>
    <?php }