1

I am trying to find a WordPress action hook that will allow me to access a new user's wp_usermeta information, immediately after the user has been created. In our registration process, custom fields are entered along with the standard WordPress user fields. So far, I've tried using the hook user_register, but that doesn't work. I sort of expected user_register wouldn't work, because its codex entry says:

https://codex.wordpress.org/Plugin_API/Action_Reference/user_register

"Not all user meta data has been stored in the database when this action is triggered. For example, nickname is in the database but first_name and last_name are not (as of 3.9.1)"

And this has turned out the be the case. My function does not return any user meta data if I use the user_regsiter hook.

The general outline of my function is:

add_action( 'user_register', 'myfunction', 10, 1 );

function myfunction ( $userid ) {

    $user_object = get_userdata( $userid );

    $user_fields = array(

                    //this is returned when using user_register hook
                    $user_object->user_login,

                    //this is *not* returned when using user_register hook
                    $user_object->some custom field
                    );

   //do stuff with $user_fields array
}

So my question is, what hook can I use that fires after a user has registered, AND after their user meta data has been written to wp_usermeta ?

  • Possible duplicate of this question which seems to have a good answer: wordpress.stackexchange.com/questions/209400/… – Dave Romsey Oct 5 '16 at 18:29
  • No, it's a different question. The question you referenced was trying to write user meta data to the wp_usermeta table. I'm trying to retrieve user meta data from the wp_usermeta table. – LeonardShelby Oct 5 '16 at 19:39
0

The edit_user_created_user will work for when adding users via user-new.php:

add_action( 'edit_user_created_user', 'wpse_edit_user_created_user', 10, 2 ); //for user-new.php page new user addition
function wpse_edit_user_created_user( $user_id, $notify ) {
    $meta = get_user_meta( $user_id, 'my_field', true );
}

For the sake of completeness, I used the following code for testing the user meta data, which I grabbed from this answer.

/**
 * Declaring the form fields
 */
function show_my_fields( $user ) {
   $fetched_field = get_user_meta( $user->ID, 'my_field', true ); ?>
    <tr class="form-field">
       <th scope="row"><label for="my-field"><?php _e('Field Name') ?> </label></th>
       <td><input name="my_field" type="text" id="my-field" value="<?php echo esc_attr($fetched_field); ?>" /></td>
    </tr>
<?php
}
add_action( 'show_user_profile', 'show_my_fields' ); //show in my profile.php page
add_action( 'edit_user_profile', 'show_my_fields' ); //show in my profile.php page

//add_action( 'user_new_form_tag', 'show_my_fields' ); //to add the fields before the user-new.php form
add_action( 'user_new_form', 'show_my_fields' ); //to add the fields after the user-new.php form

/**
 * Saving my form fields
 */
function save_my_form_fields( $user_id ) {
    update_user_meta( $user_id, 'my_field', $_POST['my_field'] );
}
add_action( 'personal_options_update', 'save_my_form_fields' ); //for profile page update
add_action( 'edit_user_profile_update', 'save_my_form_fields' ); //for profile page update

add_action( 'user_register', 'save_my_form_fields' ); //for user-new.php page new user addition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.