Tell me more ×
WordPress Answers 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

4 Answers

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 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 at 11:41

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
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.