REST API

WordPress REST API was added in version 4.7 and became the ground not only for Gutenberg block editor but also for headless web applications built on the base of WP. My crossposting plugin, for example, uses it to interact between two standalone WordPress websites.

Below you can find the tutorials about both WordPress and WooCommerce REST APIs.

Hooks When Creating or Updating a Post (Classic Editor, Gutenberg and REST API)

Probably all of us know the save_post action hook which usually runs every time a WordPress post of any type (or even a WooCommerce product) is getting created or updated.

But sometimes you need to process only post updates in some specific cases, for example only when you do it via the REST API or using the Classic Editor. Is there a way to do that?

Let’s try to figure it out.

Create or Update WooCommerce Orders via REST API

Yes, I know, I already have plenty of tutorials on this topic on my blog, for example I’ve showed you how to do the same for products, customers and so on. And now it is time to do it for WooCommerce orders.

By the way, if you’re looking for a way how you to delete orders using REST API, check this out.

How to Delete a WooCommerce Order Programmatically

In this tutorial I would actually show you how to really delete a WooCommerce order programmatically, I mean because when I tried to find info about it, I only found recommendations about using wp_delete_post() function (which we can’t use it since HPOS appeared) or woocommerce_delete_shop_order() function (there is no such function).

I needed it when I was developing a synced order deletion feature for my Multisite Order Sync plugin.

The long story short, let’s just dive straight into it.

Create a Post with WordPress REST API using JavaScript

In the previous tutorial we discussed how you can create WooCommerce products by sending REST API requests in JS, so I kind of liked this topic and decided to take a deeper look at regular WordPress posts as well, besided there is almost no similar information out there.

The main struggle here of course is to send POST API requests in JavaScript. When you need to get posts, it is simple – just use fetch() and there we go. But when we need to authenticate – how we can do that so nobody can obtain our site application data by just inspecting the code?

How to Get Posts with REST API

If you’ve never worked with WordPress REST API before, then I am definitely recommend you to start with this tutorial, in which we are going to code some basic interactions with REST API using PHP and JavaScript. And as an example we’re just going to get posts from another WordPress blog.

First thing first we need to decide which blog’s posts we are going to get. I can not use my own blog for this example because I have REST API disabled :) I think we can use Matt Mullenweg’s blog for this example who is the founder of WordPress and whose blog has an open REST API (we can easily check it if we type in the browser: ma.tt/wp-json). So, the long story short WordPress REST API allows us to get posts from almost any WordPress blog and to display them either on another website or in a mobile app.

Let’s try to visit this URL then ma.tt/wp-json/wp/v2/posts. You can see that all the latest posts are displayed in your browser tab in JSON format.

WordPress API get posts in JSON format

And maybe the next question will be – what can we do with this JSON? Ok, right now I am about to show you two examples how to work with it with PHP and JavaScript functions.

Libraries (Official) to Work with WooCommerce REST API and Examples

In the examples across my blog I almost always use the HTTP API when create REST API requests to WooCommerce. I do that because my plugins and especially the crossposting plugin uses HTTP API. You would probably agree with me that it is not always the point to include the whole PHP library inside your custom plugin.

But guys, WooCommerce has the amazingly convenient libraries to work with its REST API which you can use on your projects! In this guide we’re going to talk about PHP and JavaScript libraries (but there are also Python and Ruby ones).

How to Auto-Post From One WordPress Site to Another

In this tutorial I am going to show you how you can automatically post an article to another WordPress website with the help of a couple of hooks and REST API. There is also an alternative way of doing that with my Simple WP Crossposting plugin, we’re also going to cover in just a little bit.

By the way, in the similar tutorials about this topic it is usually recommended to use a RSS feed and a plugin which allows to import the content from a feed. Well, this may work when your post consists only from a title and some content but it won’t work of course for complex posts with custom fields, featured images etc.