0

I created a custom user_meta, but I cant update it via curl.

I can do this with user_meta

curl --header "Authorization: Basic ACCESS_TOKEN" -X POST -d "description=New description" http://domain.com/wp-json/wp/v2/users/1

But I cant do the same with custom meta

curl --header "Authorization: Basic ACCESS_TOKEN" -X POST -d "user_continent=Asia" http://domain.com/wp-json/wp/v2/users/1

JSON screenshot

This may help

How I add the custom meta to the API:

add_action( 'rest_api_init', 'adding_user_meta_rest' );

function adding_user_meta_rest() {
$user_metas = array('account_psn_title', 'account_psn_id', 'account_steam_title', 'account_steam_id', 'account_twitch_title', 'account_twitch_id', 'account_xbox_title', 'account_xbox_id', 'account_youtube_title', 'account_youtube_id', '_uw_balance', 'user_continent', 'user_platform');
    foreach ($user_metas as $user_meta) {
        register_rest_field( 'user',
            $user_meta,
            array(
                'get_callback'      => 'user_meta_callback',
                'update_callback'   => 'user_meta_update_callback',
                'schema'            => null,
            )
        );
    }

}
function user_meta_callback( $user, $field_name, $request) {
    return get_user_meta( $user[ 'id' ], $field_name, true );
}
function user_meta_update_callback( $user, $field_name, $request) {
    update_user_meta( $user[ 'id' ], $field_name, XXXX );
}

function user_meta_update_callback is my attempt to make those custom meta editable.

What should I put in user_meta_update_callback in place of the XXXX? Am I doing right?

  • is user_continent a meta you have coded yourself ? – mmm Feb 2 '18 at 23:19
  • Yes, it's. a custom user_meta. – Yamona Feb 2 '18 at 23:21
4

arguments get_callback and update_callback receive different arguments.

try this exemple which works for a field user_continent

add_action("rest_api_init", function () {


    register_rest_field(
          "user"
        , "user_continent"
        ,
        [
            "get_callback" => function ($user, $field_name, $request, $object_type) {

                return get_user_meta($user["id"], $field_name, TRUE);

            },
            "update_callback" => function ($value, $user, $field_name, $request, $object_type) {

                update_user_meta($user->ID, $field_name, $value);

            },
        ]
    );


});
1

You'll get the value as the first parameter, or you could get it out of $request, which is a WP_REST_Request object. Since you're using POST, the parameters out of $request->get_body_params() which will return an array. If you just need the actual meta value, you don't need to do this.

Your callback function should look something like this:

function user_meta_update_callback( $value, $user, $field_name, $request ) {
    update_user_meta( $user[ 'id' ], $field_name, $value );
}
  • You are right. But only user id not work. The URL contains the user id at the end "any.com/wp-json/wp/v2/users/1" – Yamona Feb 3 '18 at 1:03
  • I user $request['id'] instead of $user['id'] and it now work – Yamona Feb 3 '18 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.