How do we pass meta data when creating user with REST API via cURL?
-
I’m able to create users with a cURL command from a remote shell script. Passing data to meta does not seem to work. I’ve been trying to pass the data across as JSON. The user record is created but user_meta records don’t get created.
Can someone help me with the syntax?
-
Include an array of name: value pairs under the “meta” key. The relevant part of the passed JSON might look like:
"meta":{"my_meta_key":"my meta value"},
The meta field you add must be exposed to the API via register_meta().OK, it seems like I’m getting the meta formatted correctly.
-H "Content-Type: application/json" \ -d "{\"email\":\"masu@mailinator.com\",\"username\":\"Masu\",\"meta\":{\"wpcf-uuid\":\"E54AF95C-CC35-462C-A058-561D6CE3C108\"},...
I’ll follow up the links to register_meta(),
thanks
I’m using a call to GET /wp/user/<id> to test visibility of the custom field.
I included register_meta() in my functions.php file and that didn’t help, the custom fields weren’t returned. Should I have done more than simply paste in a call to register_meta()?
However, the method outlined on StackExchange did return the custom fields. https://wordpress.stackexchange.com/questions/338134/updating-custom-wordpress-user-meta-field-via-rest-api
add_action('rest_api_init', function() { register_rest_field( 'user', 'uuid', [ 'get_callback' => 'get_user_uuid', 'update_callback' => 'update_user_uuid', 'schema' => [ 'type' => 'string', 'description' => 'The UUID passed in by FileMaker', 'context' => [ 'view', 'edit' ], ], ]); }); function get_user_uuid( $user, $field_name, $request ) { return get_user_meta( $user[ 'id' ], $field_name, true ); } function update_user_uuid( $user, $meta_value ) { update_user_meta( $user[ 'id' ], 'wpcf-uuid', $meta_value ); }
I’ll check to see if it is possible to pass data into the UUID field when creating users. It’s working for accessing data that is currently there.
I’ve tried with register_meta() and with register_rest_field(). Currently I have both in place. This allows me to see the data when I use the GET endpoint but it doesn’t seem to help me when I use the PUT endpoint. Right now my functions.php contains this code:
/* * Allow custom field wpcf-uuid to be accessible to REST-API */ register_meta( 'user', 'wpcf-uuid', array( 'single' => true, 'type' => 'string', 'default' => true, 'show_in_rest' => true, ) ); add_action('rest_api_init', function() { register_rest_field( 'user', 'wpcf-uuid', [ 'get_callback' => 'get_user_uuid', 'update_callback' => 'update_user_uuid', 'schema' => [ 'type' => 'string', 'description' => 'The UUID passed in by FileMaker', 'context' => [ 'view', 'edit' ], ], ]); }); function get_user_uuid( $user, $field_name, $request ) { return get_user_meta( $user[ 'id' ], $field_name, true ); } function update_user_uuid( $user, $meta_value ) { update_user_meta( $user[ 'id' ], 'wpcf-uuid', $meta_value ); }
I’m trying to create a user with a cURL call that looks like this:
$ curl --user 'user:password' -X POST "https://my.domain.com/wp-json/wp/v2/users" \ -H "Content-Type: application/json" \ -d "{\"email\":\"tea3@mailinator.com\",\"first_name\":\"tea3\",\"last_name\":\"thaaba\",\"meta\":{\"wpcf-uuid\":\"got-it\"},\"name\":\"tea3 thaaba\",\"nickname\":\"teatha\",\"password\":\"A5B8128F-7\",\"slug\":\"tea3tha\",\"username\":\"tea3tha\"}"
I think you don’t have the right authentication setup. Check out this page:
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/I don’t think the default cookie authentication works from cURL, you need an authentication plugin.
Hi @bcworkz, thanks for looking at this issue.
WordPress v5.6 has brought “Application Passwords” into core, so we can use basic authentication without needing a plugin. The UI is accessible on a users profile page.
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' => [ 'title', 'custom-fields', 'revisions' ] ) ); });
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 3 years, 10 months ago by budasy.
- The topic ‘How do we pass meta data when creating user with REST API via cURL?’ is closed to new replies.