How to get author meta into post endpoint in api v2

This should work: function wpse_20160421_get_author_meta($object, $field_name, $request) { $user_data = get_userdata($object[‘author’]); // get user data from author ID. $array_data = (array)($user_data->data); // object to array conversion. $array_data[‘first_name’] = get_user_meta($object[‘author’], ‘first_name’, true); $array_data[‘last_name’] = get_user_meta($object[‘author’], ‘last_name’, true); // prevent user enumeration. unset($array_data[‘user_login’]); unset($array_data[‘user_pass’]); unset($array_data[‘user_activation_key’]); return array_filter($array_data); } function wpse_20160421_register_author_meta_rest_field() { register_rest_field(‘post’, ‘author_meta’, array( ‘get_callback’ => ‘wpse_20160421_get_author_meta’, … Read more

WordPress REST API and User meta data

There’s a lot of stuff saved as user meta that has no business being sent over the REST API, therefore the default endpoints do not include every piece of arbitrary meta automatically. If you want a piece of meta to appear in the REST API responses you need to register it with register_meta(), with show_in_rest … Read more

REST API: Display Category names in JSON?

For my needs, I customized the wp rest posts callback: function get_all_posts( $data, $post, $context ) { return [ ‘id’ => $data->data[‘id’], ‘date’ => $data->data[‘date’], ‘date_gmt’ => $data->data[‘date_gmt’], ‘modified’ => $data->data[‘modified’], ‘title’ => $data->data[‘title’][‘rendered’], ‘content’ => $data->data[‘content’][‘rendered’], ‘excerpt’ => $data->data[‘excerpt’][‘rendered’], ‘category’ => get_the_category_by_ID( $data->data[‘categories’][0] ), ‘link’ => $data->data[‘link’], ]; } add_filter( ‘rest_prepare_post’, ‘get_all_posts’, 10, 3 … Read more

PHP: authenticate for a REST request?

What do you mean by “external users”? If you only want Users, that are logged into your website, to use the API Endpoint, you can filter the “rest_authentication_errors” like described here Add this to your plugin: add_filter( ‘rest_authentication_errors’, function( $result ) { if ( ! empty( $result ) ) { return $result; } if ( … Read more

body_class REST field in WP-API

get_body_class() relies on $wp_query to do its thing, which is apparently a no-go for the JSON API. I considered creating a global object to hold the body class, and just using that to keep the body_class up to date…but that wouldn’t work without page reloads, and there would be no assurance that the object was … Read more