Opened 17 months ago
Closed 17 months ago
#19656 closed defect (bug) (worksforme)
wp_update_user() not compatible with cast-to-array WP_User objects
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Users | Version: | 3.3 |
| Severity: | normal | Keywords: | has-patch |
| Cc: |
Description
Pre-3.3, it was possible to do something like this:
$user = get_userdata( $user_id ); $user->user_pass = 'mypass'; wp_update_user( get_object_vars( $user ) ); // or (array)$user
It doesn't work anymore, because the data required by wp_update_user() is stored in $user->data rather than in $user. (The second line only works because of the new and nifty magic methods.)
It would be nice to add a little backward compatibility for this method of generating params for wp_update_user(). The attached patch is one idea - maybe something like that, plus a _doing_it_wrong(), would be sufficient.
Attachments (1)
Change History (4)
boonebgorges — 17 months ago
comment:2
boonebgorges — 17 months ago
I don't think it's worth it, when all you need to do is replace get_object_vars( $user ) with get_object_vars( $user->data ).
Yes, that's what I've done in BuddyPress http://buddypress.trac.wordpress.org/changeset/5588. I do think it's important to do something, though, in part because wp_update_user() still returns $user_id even when you pass it malformed data, which makes it hard to tell when something like a password update has failed.
We could make wp_update_user() accept WP_User instances, though.
Yeah, that amounts to about the same thing, and it'd be great. (Was a bit more work than 19656.patch, which is why I didn't do it :) ).
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from new to closed
Actually, passing all of the user fields isn't necessary. It's better to pass only the values that you want to update:
$to_update = array( 'ID' => $user_id, 'user_pass' => 'mypass' ); wp_update_user( $to_update );
This should work fine in both WP 3.2 and WP 3.3.

I don't think it's worth it, when all you need to do is replace get_object_vars( $user ) with get_object_vars( $user->data ).
We could make wp_update_user() accept WP_User instances, though.