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:

WPWeekly Episode 305 – 10up, JavaScript for WordPress Conference, and Jetpack 5.8
Jetpack 5.8 Adds Lazy Loading for Images Module
Consultants Are WordPress’ Boots on the Ground
Jetpack 5.5 Removes Syntax Highlighting and Gallery Widget for Compatibility with Upcoming WordPress 4.9 Release
WooCommerce Stores on Track to Surpass $10B in Sales This Year
Jetpack 5.4 Introduces Beta Version of New Search Module Powered by Elasticsearch for Professional Plan Users Adds Google Photos Integration, Available Now for Jetpack-Enabled Sites
Jetpack 5.3 Adds PHP 7.1 Compatibility, Better Control for WordAds Placement

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.