user meta - Existing user_meta fields not updated

Why am I not allowed to update existing user-meta fields when hooking on to edit_user_profile_update hook? I use this co

Why am I not allowed to update existing user-meta fields when hooking on to edit_user_profile_update hook? I use this code

add_action( 'edit_user_profile_update', 'xpl_registration_save' );

function xpl_registration_save( $user_id ) {
    $user = get_userdata($user_id);
    $user->add_role( 'gardner' );
    update_user_meta($user_id,'last_name','Smith');
    update_user_meta($user_id,'dogs_name','Sam');
}

This works for the last field, dogs_name, which is added, and updated. But for core fields, some other fields which have been created by another plugin, it doesn't work. How come?

Why am I not allowed to update existing user-meta fields when hooking on to edit_user_profile_update hook? I use this code

add_action( 'edit_user_profile_update', 'xpl_registration_save' );

function xpl_registration_save( $user_id ) {
    $user = get_userdata($user_id);
    $user->add_role( 'gardner' );
    update_user_meta($user_id,'last_name','Smith');
    update_user_meta($user_id,'dogs_name','Sam');
}

This works for the last field, dogs_name, which is added, and updated. But for core fields, some other fields which have been created by another plugin, it doesn't work. How come?

Share Improve this question edited Apr 4, 2019 at 14:51 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Feb 22, 2019 at 16:35 halhal 351 gold badge1 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can't change some user meta fields using edit_user_profile_update hook, because it is triggered before processing data from the form. So, you save user's meta to database, then edit form is processed and WP overrides your values. This hook is triggered after each profile edit.

You should use insert_user_meta filter (available since WP v4.4) to change or add user's meta. Filter is applied after user is created/updated, but meta has not yet been saved to the DB.

add_action( 'insert_user_meta', 'se329613_insert_user_meta', 30, 3);

/** 
 * @param array $meta {
 *     Default meta values and keys for the user.
 *
 *     @type string   $nickname
 *     @type string   $first_name
 *     @type string   $last_name
 *     @type string   $description
 *     @type bool     $rich_editing
 *     @type bool     $syntax_highlighting
 *     @type bool     $comment_shortcuts
 *     @type string   $admin_color
 *     @type int|bool $use_ssl
 *     @type bool     $show_admin_bar_front
 * }
 * @param WP_User $user
 * @param bool    $update 
 */
function se329613_insert_user_meta( $meta, $user, $update ) 
{
    // set only when adding a new user
    if ( !$update ) {
        $meta['last_name'] = 'Smith';
        $meta['dogs_name'] = 'Sam';
    }
    return $meta;
}

You can use the user_register action to perform other operations. Functions hooked to user_register are executed when everything is already saved.

add_action( 'user_register' , 'se329613_user_register' );
function se329613_user_register( $user_id ) 
{
    /* some code */
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745628031a4636929.html

相关推荐

  • user meta - Existing user_meta fields not updated

    Why am I not allowed to update existing user-meta fields when hooking on to edit_user_profile_update hook? I use this co

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信