Wordpress - Adding fields to the "Add New User" screen in the dashboard
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/
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');
You need to do 2 things.
- Register fields
- 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'] );
}
}