{"id":1998,"date":"2016-12-25T10:32:27","date_gmt":"2016-12-25T09:32:27","guid":{"rendered":"http:\/\/www.jurecuhalev.com\/blog\/?p=1998"},"modified":"2016-12-25T10:33:23","modified_gmt":"2016-12-25T09:33:23","slug":"co-authors-plus-wordpress-json-rest-api","status":"publish","type":"post","link":"https:\/\/www.jurecuhalev.com\/blog\/co-authors-plus-wordpress-json-rest-api\/","title":{"rendered":"Co-Authors Plus WordPress JSON REST API"},"content":{"rendered":"<p>I was trying to figure out if someone already wrote an REST API endpoint for WordPress Co-Authors plugin. There is <a href=\"https:\/\/github.com\/dfmedia\/wp-api-co-author-plus-endpoints\">wp-api-co-author-plus-endpoints<\/a>, 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&#8217;s very simple to extend existing REST API endpoints.<\/p>\n<p>Here&#8217;s a full snippet:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\nif ( function_exists('get_coauthors') ) {\r\n    add_action( 'rest_api_init', 'custom_register_coauthors' );\r\n    function custom_register_coauthors() {\r\n        register_rest_field( 'post',\r\n            'coauthors',\r\n            array(\r\n                'get_callback'    =&gt; 'custom_get_coauthors',\r\n                'update_callback' =&gt; null,\r\n                'schema'          =&gt; null,\r\n            )\r\n        );\r\n    }\r\n\r\n    function custom_get_coauthors( $object, $field_name, $request ) {\r\n        $coauthors = get_coauthors($object&#x5B;'id']);\r\n\r\n        $authors = array();\r\n        foreach ($coauthors as $author) {\r\n            $authors&#x5B;] = array(\r\n                'display_name' =&gt; $author-&gt;display_name,\r\n                'user_nicename' =&gt; $author-&gt;user_nicename\r\n            );\r\n        };\r\n\r\n        return $authors;\r\n    }\r\n}\r\n<\/pre>\n<p>This allows you to query <em>http:\/\/wordpress.domain.com\/wp-json\/wp\/v2\/posts\/213540<\/em> and get <em>coauthors<\/em> key back in response.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s very simple to extend existing REST API endpoints. Here&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[924],"tags":[],"class_list":["post-1998","post","type-post","status-publish","format-standard","hentry","category-wordpress-tutorial"],"acf":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/1998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/comments?post=1998"}],"version-history":[{"count":2,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/1998\/revisions"}],"predecessor-version":[{"id":2000,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/1998\/revisions\/2000"}],"wp:attachment":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/media?parent=1998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/categories?post=1998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/tags?post=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}