wp_update_user() – Updates a user in the database.

You appear to be a bot. Output may be restricted

Description

Updates a user in the database.

It is possible to update a user's password by specifying the 'user_pass' value in the $userdata parameter array. If current user's password is being updated, then the cookies will be cleared.

Usage

$int|WP_Error = wp_update_user( $userdata );

Parameters

$userdata
( array|object|WP_User ) required – An array of user data or a user object of type stdClass or WP_User.

Returns

int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.

Source

File name: wordpress/wp-includes/user.php


Lines:

1 to 100 of 245
function wp_update_user( $userdata ) {
  if ( $userdata instanceof stdClass ) {
    $userdata = get_object_vars( $userdata );
  } elseif ( $userdata instanceof WP_User ) {
    $userdata = $userdata->to_array();
  }

  $userdata_raw = $userdata;

  $user_id = isset( $userdata['ID'] ) ? (int) $userdata['ID'] : 0;
  if ( ! $user_id ) {
    return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
  }

  // First, get all of the original fields.
  $user_obj = get_userdata( $user_id );
  if ( ! $user_obj ) {
    return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
  }

  $user = $user_obj->to_array();

  // Add additional custom fields.
  foreach ( _get_additional_user_keys( $user_obj ) as $key ) {
    $user[ $key ] = get_user_meta( $user_id, $key, true );
  }

  // Escape data pulled from DB.
  $user = add_magic_quotes( $user );

  if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) {
    // If password is changing, hash it now.
    $plaintext_pass        = $userdata['user_pass'];
    $userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );

    
/**
 * Filters whether to send the password change email.
 *
 * @since 4.3.0
 *
 * @see wp_insert_user() For `$user` and `$userdata` fields.
 *
 * @param bool  $send     Whether to send the email.
 * @param array $user     The original user array.
 * @param array $userdata The updated user array.
 */
    $send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata );
  }

  if ( isset( $userdata['user_email'] ) && $user['user_email'] !== $userdata['user_email'] ) {
    
/**
 * Filters whether to send the email change email.
 *
 * @since 4.3.0
 *
 * @see wp_insert_user() For `$user` and `$userdata` fields.
 *
 * @param bool  $send     Whether to send the email.
 * @param array $user     The original user array.
 * @param array $userdata The updated user array.
 */
    $send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata );
  }

  clean_user_cache( $user_obj );

  // Merge old and new fields with new fields overwriting old ones.
  $userdata = array_merge( $user, $userdata );
  $user_id  = wp_insert_user( $userdata );

  if ( is_wp_error( $user_id ) ) {
    return $user_id;
  }

  $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );

  $switched_locale = false;
  if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) {
    $switched_locale = switch_to_user_locale( $user_id );
  }

  if ( ! empty( $send_password_change_email ) ) {
    /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
    $pass_change_text = __(
      'Hi ###USERNAME###,

This notice confirms that your password was changed on ###SITENAME###.

If you did not change your password, please contact the Site Administrator at
###ADMIN_EMAIL###

This email has been sent to ###EMAIL###

Regards,
All at ###SITENAME###
###SITEURL###'
    );

    $pass_change_email = array(

 View on GitHub View on Trac