Heer Makwana
Heer Makwana

Reputation: 905

Wordpress Add new User hooks

I want to add some custom fields to add new user in Wordpress . I am using the following hooks:

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:

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.

Upvotes: 10

Views: 19428

Answers (5)

Marcos Silva
Marcos Silva

Reputation: 69

The correct hook for saving custom field values ​​for the new user form is:

add_action('edit_user_created_user', 'your_callback');

See documentation

The user_register hook only works/ is called for users inserted via the wp_insert_user() function

user_register and edit_user_created_user are two hooks with identical description but called in different contexts

and good to add extra fields in the form to create a new user use:

add_action('user_new_form', 'your_callback');

Upvotes: 0

Benzine Ghost
Benzine Ghost

Reputation: 39

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');

Upvotes: 2

Farhan
Farhan

Reputation: 1601

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);
        }
    }

Upvotes: 19

temp
temp

Reputation: 1

Try this code in wp-includes/user.php:

do_action('profile_update', XXXX);
do_action('user_register', XXXX);

Upvotes: 0

Steven
Steven

Reputation: 212

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.

Upvotes: 3

Related Questions