Home Update User Meta with spaces using REST API - WordPress
Reply: 1

Update User Meta with spaces using REST API - WordPress

Dont Stop Learning
Dont Stop Learning Published in 2018-02-13 15:18:31Z

I'm trying to update the user meta with spaces registered on my website. I am using register_meta hook to register user meta. And for updating users using WordPress REST API I use wp_remote_post but the problem is when I tried to update a user meta with spaces it will add as array in response then it will add as new user meta.

For example the value of user meta is - community test

Sample Response:

[meta] => Array
          [agent_community] => Array
                                [0] => community
                                [1] => test

Sample Code for registering user meta.

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

Update using wp_remote_post

$update_data = array('email'=> 'test@mail.com', 'meta[agent_community]' => 'community test');
$args = array(
        'headers' => array(
                    'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
            'body' => array_filter($update_data));
$update_url = 'https://test-url.com/wp-json/wp/v2/users/17';
$response = wp_remote_post( $update_url, $args);    


Is there a way that can merge the array on user meta?

Maxim Sarandi
Maxim Sarandi Reply to 2018-02-13 15:49:24Z

implode() might help you. And you can change spaces with HTML entities. Or use function like urledncode() / urlendecode() or something like this.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.439719 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO