WordPress.org

Make WordPress Core

Opened 19 months ago

Closed 17 months ago

Last modified 9 months ago

#43051 closed defect (bug) (fixed)

Documentation fix for wp_update_user function first parameter

Reported by: nextendweb Owned by: SergeyBiryukov
Milestone: 5.1 Priority: normal
Severity: normal Version:
Component: Users Keywords: has-patch
Focuses: docs Cc:

Description

/wp-includes/users.php

wp_update_user functions expects array, but it accepts object (which converted to array) and WP_User object (which converted to array).

So I think the comment for the $userdata parameter should be improved.

Current documentation

<?php
/**
 * Update 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.
 *
 * @since 2.0.0
 *
 * @see wp_insert_user() For what fields can be set in $userdata.
 *
 * @param object|WP_User $userdata An array of user data or a user object of type stdClass or WP_User.
 * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.
 */
function wp_update_user($userdata) {
        if ( $userdata instanceof stdClass ) {
                $userdata = get_object_vars( $userdata );
        } elseif ( $userdata instanceof WP_User ) {
                $userdata = $userdata->to_array();
        }

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

Improved documentation

<?php
/**
 * Update 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.
 *
 * @since 2.0.0
 *
 * @see wp_insert_user() For what fields can be set in $userdata.
 *
 * @param array|object|WP_User $userdata An array of user data or a user object of type stdClass or WP_User.
 * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.
 */
function wp_update_user($userdata) {
        if ( $userdata instanceof stdClass ) {
                $userdata = get_object_vars( $userdata );
        } elseif ( $userdata instanceof WP_User ) {
                $userdata = $userdata->to_array();
        }

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

Attachments (1)

43051.1.patch (641 bytes) - added by nextendweb 19 months ago.

Download all attachments as: .zip

Change History (5)

#1 @subrataemfluence
19 months ago

  • Keywords has-patch added

#2 @SergeyBiryukov
17 months ago

  • Component changed from Comments to Users
  • Focuses docs added
  • Milestone changed from Awaiting Review to 5.0

#3 @SergeyBiryukov
17 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 42774:

Docs: Clarify that wp_update_user() accepts an array as $userdata argument.

Props nextendweb.
Fixes #43051.

#4 @johnbillion
9 months ago

  • Milestone changed from 5.0 to 5.1
  • Version trunk deleted
Note: See TracTickets for help on using tickets.