Co-Authors Plus WordPress JSON REST API

I was trying to figure out if someone already wrote an REST API endpoint for WordPress Co-Authors plugin. There is wp-api-co-author-plus-endpoints, but it seems that it is focused on guest authors. For my project I needed to include information about co-author users. It turns out it’s very simple to extend existing REST API endpoints.

Here’s a full snippet:

<br />
&lt;?php</p>
<p>if ( function_exists('get_coauthors') ) {<br />
    add_action( 'rest_api_init', 'custom_register_coauthors' );<br />
    function custom_register_coauthors() {<br />
        register_rest_field( 'post',<br />
            'coauthors',<br />
            array(<br />
                'get_callback'    =&gt; 'custom_get_coauthors',<br />
                'update_callback' =&gt; null,<br />
                'schema'          =&gt; null,<br />
            )<br />
        );<br />
    }</p>
<p>    function custom_get_coauthors( $object, $field_name, $request ) {<br />
        $coauthors = get_coauthors($object['id']);</p>
<p>        $authors = array();<br />
        foreach ($coauthors as $author) {<br />
            $authors[] = array(<br />
                'display_name' =&gt; $author-&gt;display_name,<br />
                'user_nicename' =&gt; $author-&gt;user_nicename<br />
            );<br />
        };</p>
<p>        return $authors;<br />
    }<br />
}<br />

This allows you to query http://wordpress.domain.com/wp-json/wp/v2/posts/213540 and get coauthors key back in response.