How do we pass meta data when creating user with REST API via cURL?

I have found a solution to my problem. I suspect that the most prominent solutions found by my web searching were suitable for /wp/v1/. We are now at /wp/v2/ so those answers are out-of-date.

The advice to use register_meta to declare that a particular meta option can be accessed by REST API is still correct.

It can easily be added by placing a short code block in your theme functions.php file. Registering your custom field using register_meta instructs your custom meta property to be included in the output when a call is made to a REST endpoint.

This is what I have included in my theme functions.php file.

add_action( 'rest_api_init', function () {   
		register_meta( 'user', 'wpcf-uuid', array( 
		'single' => true, 
		'type' => 'string', 
		'default' => true, 
		'show_in_rest' => true, 
		'supports' => [
		) );

For my needs, to fill custom fields when adding a new user, the important component that I needed was to add an action to be triggered on rest_insert_user. That can be achieved with this code, which I found at Stack Exchange – WordPress. Thanks to Guilherme Sampaio. Guilherme Sampaio

The function simply walks through the meta array and passes the values to update_user_meta.

function remote_user_update($user, $request, $create)
    if ($request['meta']) {
        $user_id = $user->ID;
        foreach ($request['meta'] as $key => $value) {
            update_user_meta( $user_id, $key, $value );
add_action( 'rest_insert_user', 'remote_user_update', 12, 3 );

This is solution may be the “first and worst.” If you have a different way of solving this problem, please respond here so that there can be a small repo of working solutions for other people working with Custom Fields and the JSON REST API.

  • This reply was modified 2 days, 9 hours ago by budasy.

Source link