Getting user meta data from WP REST API

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, "https://wordpress.stackexchange.com/". $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, "https://wordpress.stackexchange.com/". $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

Leave a Comment