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

Built from https://develop.svn.wordpress.org/trunk@51738


git-svn-id: http://core.svn.wordpress.org/trunk@51346 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2021-09-07 21:31:55 +00:00
parent aebe05f6f4
commit 25da00ea81
2 changed files with 29 additions and 2 deletions

View File

@ -1711,9 +1711,10 @@ function validate_username( $username ) {
* @since 2.0.0
* @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact
* methods for new installations. See wp_get_user_contact_methods().
* @since 4.7.0 The user's locale can be passed to `$userdata`.
* @since 4.7.0 The `locale` field can be passed to `$userdata`.
* @since 5.3.0 The `user_activation_key` field can be passed to `$userdata`.
* @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only).
* @since 5.9.0 The `meta_input` field can be passed to `$userdata` to allow addition of user meta data.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
@ -1758,6 +1759,8 @@ function validate_username( $username ) {
* as a string literal, not boolean. Default 'true'.
* @type string $role User's role.
* @type string $locale User's locale. Default empty.
* @type array $meta_input Array of custom user meta values keyed by meta key.
* Default empty.
* }
* @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
* be created.
@ -2058,6 +2061,8 @@ function wp_insert_user( $userdata ) {
*
* Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
*
* For custom meta fields, see the {@see 'insert_custom_user_meta'} filter.
*
* @since 4.4.0
* @since 5.8.0 The $userdata parameter was added.
*
@ -2084,6 +2089,28 @@ function wp_insert_user( $userdata ) {
*/
$meta = apply_filters( 'insert_user_meta', $meta, $user, $update, $userdata );
$custom_meta = array();
if ( array_key_exists( 'meta_input', $userdata ) && is_array( $userdata['meta_input'] ) && ! empty( $userdata['meta_input'] ) ) {
$custom_meta = $userdata['meta_input'];
}
/**
* Filters a user's custom meta values and keys immediately after the user is created or updated
* and before any user meta is inserted or updated.
*
* For non-custom meta fields, see the {@see 'insert_user_meta'} filter.
*
* @since 5.9.0
*
* @param array $custom_meta Array of custom user meta values keyed by meta key.
* @param WP_User $user User object.
* @param bool $update Whether the user is being updated rather than created.
* @param array $userdata The raw array of data passed to wp_insert_user().
*/
$custom_meta = apply_filters( 'insert_custom_user_meta', $custom_meta, $user, $update, $userdata );
$meta = array_merge( $meta, $custom_meta );
// Update user meta.
foreach ( $meta as $key => $value ) {
update_user_meta( $user_id, $key, $value );

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-51737';
$wp_version = '5.9-alpha-51738';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.