An experiment with the WP REST API and Jetpack’s REST API

in WordPress

A few months ago, I set to learn more about the differences between Jetpack’s REST API and the WP REST API plugin. Both projects are very interesting for the future of WordPress, as they allow third-party apps and services to interact with WordPress.

I consequently developed a little plugin using both APIs: REST API Post Embeds.

The plugin allows you to embed posts from your site or others’ into your posts and pages. It does so using Jetpack’s REST API or the WP REST API plugin.

To use the plugin, all you have to do is to add the jeherve_post_embed shortcode into one of your posts or pages. That shortcode accepts many different parameters, listed on this page. The most important parameter is wpapi, as it allows you to choose whether the plugin should query the site using Jetpack or the WP REST API plugin.

To give you an example, you could for example use the plugin to retrieve an image grid of the 8 most recent posts tagged “jetpack” on WPTavern.com, using Jetpack’s REST API.

[jeherve_post_embed url="wptavern.com" number="8" include_images="true" include_title="false" include_excerpt="false" image_size="125,125" tag="jetpack" wrapper_class="example-grid"]

I believe the shortcode parameters speak for themselves here. Here is the result:

Jetpack 5.4 Introduces Beta Version of New Search Module Powered by Elasticsearch for Professional Plan Users
WordPress.com Adds Google Photos Integration, Available Now for Jetpack-Enabled Sites
Jetpack 5.3 Adds PHP 7.1 Compatibility, Better Control for WordAds Placement
Jetpack 5.2 Brings Major Improvements to the Contact Form Module
WPWeekly Episode 282 – Talking WooCommerce with Cody Landefeld
Jetpack Professional Plan Introduces Unlimited Access to 200+ Commercial Themes
WordPress.com Introduces Scheduling for Social Media Posts
Quick Tip: How to Access Jetpack’s Alternative Module Interface
Source: wptavern.com

Another example would be to use the WP REST API to retrieve an excerpt of the last post on the tavern:

[jeherve_post_embed wpapi="true" url="wptavern.com" number="1"]

It’s as simple as that. :)

It’s been very interesting to play with both APIs. I quickly noticed some differences; it was sometimes challenging to build a plugin that would play nice with both. For example, I had to make an additional API call to get a post’s Featured image when using the WP REST API. The 2 APIs also have different ways to cope with errors, so it’s been interesting to try to recreate as many errors as possible to try to catch them all! ?

You can check the plugin’s code on GitHub. I’ve tried to comment my code as much as I could, so others can learn from it as well. I also opted to use filters whenever possible so you can extend the plugin or change its default behaviour if necessary.

Give it a try, let me know what you think, and let me know if you find bugs :)

Comments are closed.