wp_update_user doesn’t update and update_user_meta does

I’m building user profile page where users could update their profile on the front-end. User profile is extended with a few custom fields, like ‘Twitter’, ‘Facebook’ and ‘City’.

The following code successfully updates all user details except the dCity:

Read More
$user_fields = array(
 'ID'           => $current_user->ID,
 'nickname'     => esc_attr($_POST['nickname']),
 'first_name'   => esc_attr($_POST['first_name']),
 'last_name'    => esc_attr($_POST['last_name']),
 'display_name' => esc_attr($_POST['display_name']),
 'user_email'   => esc_attr($_POST['email']),
 'user_url'     => esc_attr($_POST['url']),
 'twitter'      => esc_attr($_POST['twitter']),
 'facebook'     => esc_attr($_POST['facebook']),
 'description'  => esc_attr($_POST['description']),
 'dCity'        => esc_attr($_POST['dCity'])
);

wp_update_user($user_fields) ;

I’ve tried to use update_user_meta to update dCity and it worked:

$user_fields = array(
 'ID'           => $current_user->ID,
 'nickname'     => esc_attr($_POST['nickname']),
 'first_name'   => esc_attr($_POST['first_name']),
 'last_name'    => esc_attr($_POST['last_name']),
 'display_name' => esc_attr($_POST['display_name']),
 'user_email'   => esc_attr($_POST['email']),
 'user_url'     => esc_attr($_POST['url']),
 'twitter'      => esc_attr($_POST['twitter']),
 'facebook'     => esc_attr($_POST['facebook']),
 'description'  => esc_attr($_POST['description'])
);

wp_update_user($user_fields);
update_user_meta( $current_user->ID, 'dCity', $_POST['dCity'] );

So problem solved, but I’m wondering why wp_update_user doesn’t update all records? Am I doing something wrong?

In advance, thank you.

Related posts

Leave a Reply

1 comment

  1. wp_update_user & metadata

    wp_update_user updates records in the *_users table. It isn’t meant to update custom metadata in the *_usermeta table.

    Hence your “problem” is actually expected behavior.

    The $userdata argument passed to wp_update_user can contain the following fields:

    ID, user_pass, user_login, user_nicename, user_url, user_email, display_name, nickname, first_name, last_name, description, rich_editing, user_registered, role, show_admin_bar_front

    Further it accepts metadata that is recognized as a “contact method”. This it will save to the *_usermeta table.

    By default, those are jabber, aim & yim.

    These can however be altered / appended to by means of the 'user_contactmethods' filter. As for the question raised in the comments, that is likely how twitter & facebook metadata have been made saveable via wp_update_user.

    Any additional metadata should be saved as such with the appropriate functions.

    How does it happen in the core?

    This just as an aside:

    wp_update_user calls _get_additional_user_keys which in turn calls _wp_get_user_contactmethods. The resulting metakeys are iterated over back in wp_update_user and if a value exists added to the *_usermeta table via update_user_meta.

    For reference, see Line 1426 and after (as of 3.5.1) of /wp-includes/user.php