I'm trying to work with the Wordpress API to update a user's information via a POST request. The information is stored in a custom JSON field, that I registered like so:
public function handle_user_info() {
register_rest_field( 'user', 'personal_info', array(
'get_callback' => array( $this, 'get_user_info_callback' ),
'update_callback' => array( $this, 'post_user_info_callback' ),
'schema' => null
));
}
Here's the GET callback:
public function get_user_info_callback( $user ) {
$userID = $user[ 'id' ];
return array(
'rcp_user_first' => get_user_meta( $userID, 'rcp_user_first', true ),
'rcp_user_last' => get_user_meta( $userID, 'rcp_user_last', true ),
'rcp_user_location' => get_user_meta( $userID, 'rcp_user_location', true ),
'rcp_postal_address' => get_user_meta( $userID, 'rcp_postal_address', true ),
'rcp_email' => get_userdata( $userID )->user_login
);
}
All of the above works fine. When I make a GET request, here's what is returned (shortened version):
{
"id": 1,
"name": "John",
"url": "http://example.com",
"description": "",
"slug": "admin",
...
"personal_info": {
"rcp_user_first": "John",
"rcp_user_last": "Doe",
"rcp_user_location": "EU",
"rcp_postal_address": "101 Fake Street",
"rcp_email": "admin@example.com"
}
...
}
My problem is with the POST callback. I can't figure out how to write it to update the values in the personal_info
field. My guess was that something like this would work:
public function post_user_info_callback( $value, $user, $fieldName ) {
return array(
'rcp_user_first' => update_user_meta( 1, 'rcp_user_first', 'Mark' ),
'rcp_user_last' => update_user_meta( 1, 'rcp_user_last', 'Smith' ),
'rcp_user_location' => update_user_meta( 1, 'rcp_user_location', 'NON_EU' ),
'rcp_postal_address' => update_user_meta( 1, 'rcp_postal_address', '202 Fantasy Street' )
);
}
However this does not work. By the way I'm only testing with Postman right now which is why I'm using static data, so 1 here stands for the user ID.
Any ideas?