Wordpress Add new User hooks
Solution 1
As far as I can see there are no action hooks that will trigger on the new user page. Searched in user-new.php for do_action
.
Solution 2
Use this hook
add_action('user_new_form', 'xxxx');
Ok, here is the full code to add a permission checkbox for user mailChimp registration on user add/edit
//Add a mailchimp permission field, on user creation, user profile update
add_action('user_new_form', 'mailchimp_permission_field');
add_action('show_user_profile', 'mailchimp_permission_field');
add_action('edit_user_profile', 'mailchimp_permission_field');
function mailchimp_permission_field($user) {
?>
<table class="form-table">
<tr class="form-field">
<th scope="row"><label for="mail_chimp">Mail Chimp </label></th>
<td>
<label for="mail_chimp">
<input style="width: auto;" type="checkbox" name="mail_chimp" id="mail_chimp"
<?php if(current_filter() == 'user_new_form' || get_the_author_meta('mail_chimp', $user->ID )): ?>
checked = "checked"
<?php endif; ?> />
Subscribe to MailChimp.
</label>
</td>
</tr>
</table>
<?php }
// handle mailchimp registrations on user creation
add_action( 'user_register', 'subscribe_to_mailchimp_after_registration', 10, 1 );
function subscribe_to_mailchimp_after_registration( $user_id ) {
if (isset($_POST['email']) && isset($_POST['mail_chimp']) && $_POST['mail_chimp'] == 'on') {
mailchimp_subscribe($_POST['email']);
}
}
//Save new field for user in users_meta table
add_action('user_register', 'save_mailchimp_permission_field');
add_action('edit_user_profile_update', 'save_mailchimp_permission_field');
function save_mailchimp_permission_field($user_id) {
if (!current_user_can('edit_user', $user_id)) {
return false;
}
if (isset($_POST['mail_chimp']) && $_POST['mail_chimp'] == 'on') {
update_usermeta($user_id, 'mail_chimp', true);
mailchimp_subscribe(get_userdata($user_id)->user_email);
}
else {
update_usermeta($user_id, 'mail_chimp', false);
mailchimp_unsubscribe(get_userdata($user_id)->user_email);
}
}
Solution 3
According to the documentation, you can hook
to the user_new_form
action, of course your WordPress version should be above version 3.7.0.
This hook fires at the end of the new user form. It passes a contextual string to make both types of new user forms uniquely target-able. Contexts are add-existing-user
(Multi-site) and add-new-user
(single site and network admin).
add_action('user_new_form', 'your_function_name');
Heer Makwana
Updated on June 18, 2022Comments
-
Heer Makwana almost 2 years
I want to add some custom fields to add new user in Wordpress . I am using the following hooks:
show_user_profile
edit_user_profile
these hooks displaying new custom field on edit profile page, but i want the new custom field to be shown in Add New User page.
and also i want to insert values in wp_usermeta table, for this i am using following hooks:
personal_options_update
edit_user_profile_update
these hooks are also working fine on edit or update profile, but i need the insertion of record in wp_usermeta tabe at the time of Add new User, not at the Profile update time.
please give me hint of hook that will b used at ADD New User.
Thank you in advance.
-
Heer Makwana about 12 yearsYes no hooks are there to be fired on new user page and edit_user_profile_update will be fired on user_update page. Thank you.
-
bestprogrammerintheworld over 10 yearsI think this was OP was initially looking for: codex.wordpress.org/Plugin_API/Action_Reference/user_register
-
Dhara about 10 yearsHi! You can make your answer better by adding more information to it (do you have sources or links to documentation for example?)
-
Farhan about 10 years@Dhara I have added full code now, let me know if you need more explanation.
-
Hardy Mathew over 8 yearsis this also wored for admin user Add?