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
4 Answers
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.
-
I want to avoid hacking the core, so maybe I will choose the jQuery solution. It is okay if it's not very good practice, As long as it works :D, thanks for the solution Bainternet– PermanaCommented May 31, 2011 at 6:45
-
-
Personally, I would say the jQuery approach is better practice than hacking core. Once you have modified core, upgrading becomes a chore. Commented May 31, 2011 at 10:44
-
@Bainternet: Where do some one put that
$_post['action'] == 'adduser'
and How can get the last inserted wordpress user's id to update the user extra field in custom table. You should expend your answer. Commented Oct 6, 2012 at 10:08 -
I think the action now is "createuser". At least I've got this while debugging the admin user creation process. Commented Jan 30, 2020 at 10:09
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.
-
-
He was looking for action hooks on user add form. This seems to be a new one since first answers were posted above. A code example that expands on this hook is here: stackoverflow.com/a/23067428/503621– B. SheaCommented Oct 4, 2015 at 2:37
-
More specifically: this hook allows you to add a field to page he requested. Which was the question..– B. SheaCommented Oct 4, 2015 at 2:47
-
I understand but simply pasting code is only helpful to people with experience enough to understand the code.– s_ha_dumCommented Oct 4, 2015 at 2:48
-
1The easiest solution of all time. thanks a ton, man. Commented May 19, 2020 at 17:50
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( '~<label\s+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');
See this answer. There are no hooks, but you can do it with jQuery. Simply add your html markup with jQuery and it will be done.