Getting user meta data from WP REST API

I am working on a project using the WordPress REST API and VueJS along with Axios (for the AJAX calls to GET/POST data back to the site). However, i need to be able to access a user’s meta data ie ‘collapsed_widgets’ from the REST API but the data I receive back from the GET response is

"meta": [],

This is for ALL users too, which is weird becauase there should be data in there.

Is there anyway at all to get the meta data for users?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Look into register_rest_field() to register meta with the rest api.

    add_action( 'rest_api_init', 'adding_user_meta_rest' );

    function adding_user_meta_rest() {
       register_rest_field( 'user',
                            'collapsed_widgets',
                             array(
                               'get_callback'      => 'user_meta_callback',
                               'update_callback'   => null,
                               'schema'            => null,
                                )
                          );
    }

And then put your get_user_meta bit in the callback.

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

The WP_REST_Meta_Fields class may provide more useful insight as well.


Update per OP comment on registering custom routes

A quick and dirty partial example. Cobbled out of some stuff sitting in front of me, but it is not a working example.

May help as you read through docs. Follow the connection between the first register_rest_route, it’s GET method’s callback, my_get_callback, below, and the callback method’s use of the WP_Rest_Request class. It should help to draw a connection of the steps. The docs I mentioned in the comments will get into other args, params, etc., and of course the permissions_callback stuff.

Hope it helps.

        class My_Extend_Rest extends WP_REST_Controller {
            public function __construct() {
                add_action( 'rest_api_init', array( $this, 'register_routes' ) );
            }//end __construct

            public function register_routes() {
                $version = '1';
                $namespace = 'my-fancy-namespace/v' . $version; 
                $base = 'my-route-base';

    // so, site.com/wp-json/my-fancy-namespace/v2/my-route-base/

                register_rest_route( $namespace, '/'. $base, array(
                    array(
                        'methods'  => 'GET',
                        'callback' => array( $this, 'my_get_callback' ),
                        'permission_callback' => array( $this, 'key_permissions_check' ),
                        ),
                    array(
                        'methods'  => 'POST',
                        'callback' => array( $this, 'my_post_callback' ),
                        'permission_callback' => array( $this, 'key_permissions_check' ),
                        ),)
                );

                $base2 = 'my-second-base';
    // so, site.com/wp-json/my-fancy-namespace/v2/my-second-base/

                register_rest_route( $namespace, '/'. $base2, array(
                    array(
                        'methods'  => 'GET',
                        'callback' => array( $this, 'my_get_callback_two' ),
                        'permission_callback' => array( $this, 'key_permissions_check' ),
                        ),
                    array(
                        'methods'  => 'POST',
                        'callback' => array( $this, 'my_post_callback_two' ),
                        'permission_callback' => array( $this, 'key_permissions_check' ),
                        ),)
                );

        }//register_routes

       public function key_permissions_check() {
          //do permissions check stuff
       }

       public function my_get_callback( WP_REST_Request $request ) {

            //do stuff with $request 
           //see the methods mentioned in the comment




        }//end 
}//end class


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x