3

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:

$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.

  • you can install the WP_DEBUG plugin and then dig into what's happening behind the scene, error messages (if any) and I think that will give you some clue about this strange scenario – Hasin Hayder Mar 24 '13 at 8:41
  • I think it has something to do with how data is saved to wp database, there are two separated tables "wp_users" and "wp_usermeta". – Dameer Mar 24 '13 at 9:20
8

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

  • I am curious to know, in that case how his "twitter" and "facebook" fields are saved? – Hasin Hayder Mar 24 '13 at 9:35
  • Thanks for your comment. That makes sense, but I'm also curious how "twitter" and "facebook" fields are saved? Also Codex says wp_update_user() - This function updates a single user in the database. This update can contain multiple pieces of user metadata as an array. To update a single piece of user metadata, use update_user_meta() instead. – Tomas Mar 24 '13 at 9:39
  • 1
    See the appended answer, @Hasin – Johannes Pille Mar 24 '13 at 10:18
  • As for the codex: I might be on the wrong track here and have missed the spot in the functions in question where arbitrary metadata is saved, but having read their source thoroughly, it is much more likely the codex page contains misleading info. The codex is neither complete nor always correct. – Johannes Pille Mar 24 '13 at 10:23
  • 1
    @JohannesPille The codex is definitely incorrect, or rather provides a poor explanation of wp_update_user. This function has tripped me up in the past. It will allow you to update values that exist in both the wp_users and wp_usermeta tables but only those values which are defined as default by WordPress, e.g. for usermeta -> first_name, last_name, nickname, description, rich_editing, comment_shortcuts, admin_color, use_ssl, show_admin_bar_front but not any custom-custom-field, if that isn't confusing enough to the end user. – userabuser Mar 28 '14 at 6:04

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.