Make WordPress Core


Ignore:
Timestamp:
09/07/2021 09:30:07 PM (3 years ago)
Author:
johnbillion
Message:

Users: Introduce a meta_input argument for wp_insert_user().

This allows custom user meta values to be provided when creating or updating a user in the same way custom post meta can be provided to wp_insert_post() when creating or updating a post.

Also introduces the insert_custom_user_meta to filter these values.

Props desrosj, donmhico

Fixes #41950

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r51657 r51738  
    17121712 * @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact
    17131713 *              methods for new installations. See wp_get_user_contact_methods().
    1714  * @since 4.7.0 The user's locale can be passed to `$userdata`.
     1714 * @since 4.7.0 The `locale` field can be passed to `$userdata`.
    17151715 * @since 5.3.0 The `user_activation_key` field can be passed to `$userdata`.
    17161716 * @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only).
     1717 * @since 5.9.0 The `meta_input` field can be passed to `$userdata` to allow addition of user meta data.
    17171718 *
    17181719 * @global wpdb $wpdb WordPress database abstraction object.
     
    17591760 *     @type string $role                 User's role.
    17601761 *     @type string $locale               User's locale. Default empty.
     1762 *     @type array  $meta_input           Array of custom user meta values keyed by meta key.
     1763 *                                        Default empty.
    17611764 * }
    17621765 * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
     
    20582061     *
    20592062     * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
     2063     *
     2064     * For custom meta fields, see the {@see 'insert_custom_user_meta'} filter.
    20602065     *
    20612066     * @since 4.4.0
     
    20842089     */
    20852090    $meta = apply_filters( 'insert_user_meta', $meta, $user, $update, $userdata );
     2091
     2092    $custom_meta = array();
     2093    if ( array_key_exists( 'meta_input', $userdata ) && is_array( $userdata['meta_input'] ) && ! empty( $userdata['meta_input'] ) ) {
     2094        $custom_meta = $userdata['meta_input'];
     2095    }
     2096
     2097    /**
     2098     * Filters a user's custom meta values and keys immediately after the user is created or updated
     2099     * and before any user meta is inserted or updated.
     2100     *
     2101     * For non-custom meta fields, see the {@see 'insert_user_meta'} filter.
     2102     *
     2103     * @since 5.9.0
     2104     *
     2105     * @param array   $custom_meta Array of custom user meta values keyed by meta key.
     2106     * @param WP_User $user        User object.
     2107     * @param bool    $update      Whether the user is being updated rather than created.
     2108     * @param array   $userdata    The raw array of data passed to wp_insert_user().
     2109     */
     2110    $custom_meta = apply_filters( 'insert_custom_user_meta', $custom_meta, $user, $update, $userdata );
     2111
     2112    $meta = array_merge( $meta, $custom_meta );
    20862113
    20872114    // Update user meta.
Note: See TracChangeset for help on using the changeset viewer.