Manage your Custom Post Types on WordPress.com

in WordPress

I’ve talked about the WordPress.com REST API on this blog before. I even created a plugin allowing you to make use of it on your own site. This API powers WordPress.com, the desktop WordPress.com apps, and is used in more and more areas of the Jetpack plugin.

Did you know that API also allows you to manage your Custom Post Types via the WordPress.com interface?

Make your Custom Post Type available in the WordPress.com REST API

By default, that API allows you to query Posts and Pages on your site, but won’t query Custom Post Types. Luckily, Jetpack includes a filter you can use to whitelist some of your Custom Post Types, to make them available in the WordPress.com REST API. To use that filter, you can paste the following in a functionality plugin:

/**
 * Filter the list of Post Types available in the WordPress.com REST API.
 *
 * @param array $allowed_post_types Array of whitelisted Post Types.
 * @return array $allowed_post_types Array of whitelisted Post Types, including our 'ghactivity_event' Custom Post Type.
 */
function jeherve_allow_post_type_wpcom( $allowed_post_types ) {
    $allowed_post_types[] = 'ghactivity_event';
    return $allowed_post_types;
}
add_filter( 'rest_api_allowed_post_types', 'jeherve_allow_post_type_wpcom' );

Once you’ve done so, your content will be available in the WordPress.com REST API. If you go to WordPress.com/posts, or if you install one of the WordPress.com desktop apps, you’ll see the Post type in your sidebar:

Custom Post Types in the WordPress.com interface

You will then be able to create new posts or edit existing ones right from WordPress.com, without having to log in to your site.

Magic!

  1. So you can have a non wordpress.com blog, and add support to the wordpress.com app for custom post types that aren’t supported by wordpress.com?

    If so that’s really clever. Presumably any custom properties aren’t managed. How did you set the icon in the editor? Is the icon just a generic one for unrecognised post types?

    • Presumably any custom properties aren’t managed.

      Yeah, post meta will require some more work before it’s ready. See this issue if you’re interested:
      https://github.com/Automattic/wp-calypso/issues/587

      How did you set the icon in the editor? Is the icon just a generic one for unrecognised post types?

      That’s a default icon. It would be nice if Calypso could pull the Dashicon you chose in wp-admin though. You should create a GitHub issue about that. If you don’t, I will :)

Comments are closed.