6

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?

  • Just FYI. I think this was taken OUT of the API as you could be exposing some sensitive data if user meta fields automatically output. I wish there was a simple way to whitelist meta_fields, without going to the trouble of creating a callback for each field you want to add. – Armstrongest Dec 7 '17 at 1:04
5

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
  • Ok, excellent! This was what i needed but after some testing and some thought because i need to be able to send data back to the API for updating, I think what I will need to do now is register 3 custom routes. One for 'collapsed_widgets, one for 'selected_categories', and one for 'quick_links'. Do you happen to have any examples for those while I am working on them? :) – Zach Tackett Jun 15 '17 at 15:33
  • I usually extend the WP_Rest_Controller class, so look at that for sure. And then check out the WP_Rest_Request class and its many methods. – hwl Jun 15 '17 at 15:55
  • I added a code block that has some example stuff for context on registering custom routes. Hope it helps! – hwl Jun 15 '17 at 16:30

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.