Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

6 Answers 6

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');
share|improve this answer
1  
Now we are waiting for your explanation. –  toscho Jan 9 '13 at 21:19
    
I saw the source code in file user-new.php e don't have a action hook like "add_user_profile" so I simulate this with the action "admin_footer_text" and filter by $pagenow == "user-new.php". Now I commented the hack to explain the code. –  NkR Jan 10 '13 at 11:41
2  
Why you do not use user_new_form action? –  Sazzad Tushar Khan May 7 at 0:11
    
@SazzadTusharKhan tx for the pointer –  alex Sep 23 at 20:02

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.

share|improve this answer

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

share|improve this answer
    
Unfortunately, in order to get it to work, temporarily, I was forced to modify user-new.php. This is a no no. But hopefully it's temporary. –  Zach Shallbetter Jul 25 '11 at 20:55

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');

share|improve this answer
    
A code-only answer is a bad answer. Try linking related Codex article and explaining the code here. –  Mayeenul Islam Feb 6 at 9:56
up vote 0 down vote accepted

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);
share|improve this answer

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 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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