0

I'm struggling here with a question. I'm sending a POST to http://localhost/wp-json/v2/wp/users/ with this JSON data:

{
    "username" : "johndoe",
    "email": "jondoe@gmail.com",
    "password": "qwerty",
    "meta": {
        "icq": "11223344"
    }

}

But when I go to see the result, the meta object is blank. I accomplished the goal using a plug-in, but this plug-in authenticates with cookie and I have another plug-in who uses JWT to authenticate, so I think is to much plug-ins to one single task.

Someone have the same problem? Not even in official docs from the plug-in I found a solution.

  • The function register_rest_field create a new field displayed in the REST response. But the value in the meta value of the user is something else, do you manage this somewhere in your code ? – mmm Jan 11 '18 at 18:22
  • No, just this code actually. I also have another code to show the custom field in /wp-admin/ and by there I can update, but no by the Rest API. – Danilo P. da Silva Jan 11 '18 at 18:38
  • to set the default value of the custom field, you can do it on the action user_register. this action is for all creations of user, by the backend or by a REST call. – mmm Jan 11 '18 at 18:54
  • I made the things more simple now. Just trying to create a new user and pass the object meta into. But, response is blank for the object. Can you figure out why or how to? – Danilo P. da Silva Jan 12 '18 at 19:36
  • edit your question to detail the code that you use – mmm Jan 12 '18 at 21:14
2

Just found the solution in another question of Stack Overflow. Using the function register_meta():

register_meta('user', 'icq', array(
  "type" => "string",
  "show_in_rest" => true
));

Now I can make a request using:

{
    "username" : "johndoe",
    "email": "jondoe@gmail.com",
    "password": "qwerty",
    "meta": {
        "icq": "11223344"
    }

}

And the response are:

{
    "id": 49,
    "username": "johndoe",
    "name": "johndoe",
    "first_name": "",
    "last_name": "",
    "email": "johndoe@gmail.com",
    "url": "",
    "description": "",
    "link": "http://localhost/author/johndoe/",
    "locale": "en_US",
    "nickname": "johndoe",
    "slug": "johndoe",
    "roles": [
        "subscriber"
    ],
    "registered_date": "2018-01-13T11:53:57+00:00",
    "capabilities": {
        "read": true,
        "level_0": true,
        "subscriber": true
    },
    "extra_capabilities": {
        "subscriber": true
    },
    "avatar_urls": {
        "24": "http://2.gravatar.com/avatar/29a1df4646cb3417c19994a59a3e022a?s=24&d=mm&r=g",
        "48": "http://2.gravatar.com/avatar/29a1df4646cb3417c19994a59a3e022a?s=48&d=mm&r=g",
        "96": "http://2.gravatar.com/avatar/29a1df4646cb3417c19994a59a3e022a?s=96&d=mm&r=g"
    },
    "meta": {
        "icq": [
            "11223344"
        ]
    },
    "_links": {
        "self": [
            {
                "href": "http://localhost/wp-json/wp/v2/users/49"
            }
        ],
        "collection": [
            {
                "href": "http://localhost/wp-json/wp/v2/users"
            }
        ]
    }
}

And if I want to show/edit in /wp-admin/ I just use this function too:

function more_contactmethods( $contactmethods ) {
    $contactmethods['icq'] = 'ICQ';
    return $contactmethods;
}

add_filter( 'user_contactmethods', 'more_contactmethods' );

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.