database - How to Access wp_usermeta Data Immediately After a New User is Created

admin2025-01-07  3

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:

"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 ?

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 ?

Share Improve this question edited Jan 28, 2017 at 22:08 Dave Romsey 17.9k11 gold badges55 silver badges70 bronze badges asked Oct 5, 2016 at 14:26 LeonardShelbyLeonardShelby 1651 gold badge4 silver badges13 bronze badges 2
  • Possible duplicate of this question which seems to have a good answer: wordpress.stackexchange.com/questions/209400/… – Dave Romsey Commented Oct 5, 2016 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 Commented Oct 5, 2016 at 19:39
Add a comment  | 

1 Answer 1

Reset to default 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
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262089a791.html

最新回复(0)