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.
user_new_form
is the hook that can do the magic here.For more details visit my blog post: http://scriptbaker.com/adding-custom-fields-to-wordpress-user-profile-and-add-new-user-page/
I had the same need and created the following hack:
You need to do 2 things.
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:
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:
Complete Code Snippet:
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:
user_contactmethods
filter hook does not get called at theuser-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
I workaround is available by using the
user_new_form_tag
which resides inside theuser-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:The
user_new_form_tag
is situated inuser-new.php
around line 303 (in WP3.5.1 at least):Of course the downside here is that all your custom field must appear first in the form, before the fields declared in WP core.
The following code will display “Biographical Info” in “Add User” form
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
I also added the information to functions.php
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: