WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#19656 closed defect (bug) (worksforme)

wp_update_user() not compatible with cast-to-array WP_User objects

Reported by: boonebgorges Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.3
Component: Users Keywords: has-patch
Focuses: 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)

19656.patch (490 bytes) - added by boonebgorges 2 years ago.

Download all attachments as: .zip

Change History (4)

boonebgorges2 years ago

comment:1 scribu2 years 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 ).

We could make wp_update_user() accept WP_User instances, though.

comment:2 boonebgorges2 years 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 :) ).

comment:3 scribu2 years ago

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

Note: See TracTickets for help on using tickets.