Wordpress Add new User hooks
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); } }
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');