I have a native app project that gets all data from /wp-json/wp/v2.

I've registered phone using this

register_meta('user', 'phone', array(
    "type" => "string",
    "show_in_rest" => true

to the functions.php and I can see the user metadata using this


How can I create/update the usermeta using WP REST API outside WordPress?

any help will be great.



3 Answers 3


I had to do this yesterday, here's how I did it.

Like you have already done register_meta to appear in the api.

register_meta('user', 'meta_key', array(
    "type" => "string",
    "show_in_rest" => true,
    "single" => true,

Then you will need to make a POST or PUT request to the users endpoint with the meta values in the body of the request.

I accomplished this using javascript's fetch api but you could also do it with ajax or using WordPress's wp_remote_request() function.

First I enqueued my javascript.

wp_register_script( 'theme-js', get_theme_file_uri( './dist/scripts/main.js' ), [ 'jquery' ], '', true );
        'root'        => esc_url_raw( rest_url() ), // Rest api root
        'nonce'       => wp_create_nonce( 'wp_rest' ), // For auth
        'currentUser' => get_current_user_id() ?: false,
wp_enqueue_script( 'theme-js' );

Then I setup my fetch function.

const fetchApi = function(route, data, method) {
  if (!route) route = 'posts'
  if (!method) method = 'GET'

  return fetch(wpApiSettings.root + 'wp/v2/' + route, {
    credentials: 'same-origin',
    body: JSON.stringify(data),
    headers: {
      'X-WP-Nonce': wpApiSettings.nonce,
      'Content-Type': 'application/json'

Then call the function when you need.

    meta: {
      meta_key: value
  .then(resp => {
    if (resp.ok) {
      return resp.json()
    return false
  .then(json => console.log(json))

With Wordpress Version 6.4.3 I had to do the following:

$userMetaProp = 'customMeta';
add_filter('rest_pre_insert_user', static function (
    stdClass        $prepared_user,
    WP_REST_Request $request
) use ($userMetaProp): stdClass {
    if (!empty($request['meta'])) {
        $prepared_user->{$userMetaProp} = [];
        foreach ($request['meta'] as $key => $value) {
            $prepared_user->{$userMetaProp}[$key] = $value;
    return $prepared_user;
}, accepted_args: 2);
add_filter('insert_custom_user_meta', static function (
    array   $custom_meta,
    WP_User $user,
    bool    $update,
    array   $userdata
) use ($userMetaProp): array {
    if (array_key_exists($userMetaProp, $userdata)) {
        foreach ($userdata[$userMetaProp] as $key => $value) {
            $custom_meta[$key] = $value;
    return $custom_meta;
}, accepted_args: 4);

Now you can update user meta data with a request like so:

curl --location --request POST 'http://localhost/wp-json/wp/v2/users/1' \
--header 'Authorization: Basic [AUTH-TOKEN]' \
--header 'Content-Type: application/json' \
--data-raw '{
    "meta": {

You need to change [AUTH-TOKEN] with your own token (see: https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/)


  • the first filter rest_pre_insert_user get the data in the meta prop of the request and store it in $prepared_user
  • the second filter insert_custom_user_meta use the data in $userdata which get his data from $prepared_user to return the custom meta data to store in the user

I found these 2 post helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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