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, using Jetpack’s REST API.

[jeherve_post_embed url="" 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 6.8 Adds Gutenberg Blocks for Payment Buttons, Forms, Maps, and Markdown and Jetpack Launch New Activity Feature for Monitoring Website Changes
Jetpack 6.6 Improves Site Verification Tools, Asset CDN Module Now in Beta, Gutenberg Blocks Coming Soon
Facebook Shuts Down API for Publishing to User Timelines, Impacts Jetpack’s Publicize Feature
Jetpack 6.1, Now With Even More Privacy Information
WPWeekly Episode 314 – Getting Squeebly With It
Jetpack 6.0 Takes Steps Towards GDPR Compliance
WPWeekly Episode 307 – Thirty Percent of the Web

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="" 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.