Add extra field when admin create user

I have created extra field that user can edit on their ‘Your Profile’ page. The extra field also available on WordPress register page. What I want to ask is, how to add the field to User > Add new page ? So, when admin creates a user, admin also input the user’s extra field

Related posts

Leave a Reply

4 comments

  1. With no hooks on that page (User > Add new page) other then user_new_form_tag its not possible to add new fields unless you hack the core file wp-admin/user-new.php .

    You could try adding that extra field by adding in in JQuery and processing it when the $_post['action'] == 'adduser' but that wont be very good practice.

  2. This is older question but there is this now.. end of form though (only).

    WP Version 4.3.0 – File “./wp-admin/user-new.php” – Line 330

    /**
     * Fires at the end of the new user form.
     *
     * Passes a contextual string to make both types of new user forms
     * uniquely targetable. Contexts are 'add-existing-user' (Multisite),
     * and 'add-new-user' (single site and network admin).
     *
     * @since 3.7.0
     *
     * @param string $type A contextual string specifying which type of new user form the hook follows.
     */
    do_action( 'user_new_form', 'add-existing-user' );
    

    Short example:

    add_action('user_new_form', 'addmycustomfield');
    function addmycustomfield() {
    ?>
    <table class="form-table">
    <tr>
    <th scope="row">My test field:</th>
    <td><input type="text" size="32" name="testfield" value="<?php echo get_option('madeup_wp_option'); ?>" /></td>
    </tr>
    </table> 
    <?php
    }
    

    This will place a field on end of add user form.
    How to save custom user meta data/etc from registration/add user/profile edit/etc is beyond scope of the question and has been answered many times previously. See comments as well.

  3. I was able to do this through a rather fragile hack. It uses the output buffer to insert the new field. Thankfully there’s a nice hook to save the field.

    // FRAGILE!  This rewrites code directly in the buffer, upgrading WP may break this
    function wpse18772_add_fields( $buffer ) {
      $input_html = '<input type="text" name="your-new-field" id="your-new-field">';
    
      // will insert a new field after "role"
      $buffer = preg_replace( '~<labels+for="role">(.*?)</tr>~ims', '<label for="role">$1</tr><tr class="form-field"><th>My New Field</th><td>' . $input_html . '</td></tr>', $buffer );
    
      return $buffer;
    }
    
    function wpse18772_buffer_start() { ob_start("wpse18772_add_fields");  }
    function wpse18772_buffer_end() { ob_end_flush(); }
    add_action('admin_head', 'wpse18772_buffer_start', 10, 1);
    add_action('admin_footer', 'wpse18772_buffer_end', 10, 1);
    
    function wpse18772_save_fields($user_id) {
      if($_POST['action'] != "adduser" && $_POST['action'] != "createuser") return;
    
      if(!empty($_POST['your-new-field'])) {
        // save field to meta, or do whatever you want to here
      }
    }
    add_action('user_register', 'wpse18772_save_fields');