Automate the busywork

Create a WordPress Post Through the REST API

WordPress includes a REST API that lets a script or automation create posts without opening the editor. You send a POST request to the site’s posts address with a title, content and status, and you identify yourself with an application password. The response returns the new post’s ID and link. This article walks through the pieces, using drafts so nothing goes live by accident.

The details below describe the API as of this writing. WordPress changes over time, so confirm names and behavior in the official developer documentation.

What you need first

You need a site running a recent version of WordPress with the REST API available. It is on by default on most installations, though some security plugins or hosts restrict it.

You also need a user account with permission to create posts, and an application password for that user. An application password is a separate credential, generated in the user’s profile, meant for programs. It can be revoked without changing the user’s normal password.

Create one for each automation and name it after the job, such as “publishing script.” Copy it when it appears, because WordPress will not show it again. Store it as a secret and never paste it into shared files.

Find the address

The API lives under the site’s address, in a path that ends in wp-json. The posts address is typically your site address followed by /wp-json/wp/v2/posts.

Test it by opening that address in a browser. If the API is reachable, you will see JSON describing recent public posts. If you see an error or a login page, something is blocking the API, and you should check your host or security plugin settings.

Sites using plain permalinks may need an alternative form of the address, which the developer documentation explains.

Send your first request

To create a post, send a POST request to the posts address. Include a header marking the body as JSON and authenticate with basic authentication, using the username and the application password.

The body is a small JSON object. The fields you will use most are:

Set the status to draft while you are testing. If the request succeeds, the reply has a 201 status and includes the new post’s ID and a link to edit it. Open that post in the editor and confirm it looks right.

Content and formatting

The content field expects HTML, or the block markup the editor uses. If you write in Markdown, convert it to HTML before sending; many libraries do this in one line.

Keep an eye on special characters. Quotation marks and line breaks inside a JSON string must be escaped, or the request will fail. Building the body with a JSON library, instead of joining text by hand, avoids most of these errors.

Block markup is more precise but harder to write by hand. Plain HTML works fine for most posts, and the editor will treat it as a classic block that you can convert if needed.

Categories, tags and slugs

Posts refer to categories and tags by numeric ID, not by name. To find an ID, send a GET request to the categories address and look for the name you want. Save the IDs somewhere your script can read them, or look them up each run.

If you send a category ID that does not exist, the post is created without it, which can be a quiet failure. Check the response to confirm the categories were applied.

Provide a slug if you want control over the URL. Otherwise WordPress builds one from the title. Choose short, readable slugs and avoid changing them later, since changed addresses can break links.

Images take two steps. First, upload the file to the media address with a POST request, sending the file as the body and a header with its filename. The response returns a media ID.

Second, create or update the post with a featured_media field set to that ID. If you also want alt text, update the media item with an alt_text field.

Resize and compress images before uploading, since large files slow the site. Do this in the same workflow as a separate step, so it is never forgotten.

Handling errors

When something fails, read the status code and body. A 401 means your credentials were not accepted, so check the username, the password and that you used basic authentication. A 403 means you lack permission, possibly because the user role cannot create posts. A 400 usually points to a bad field, and the message often names it. A 404 means the address is wrong or the API is disabled.

Log the status and body of every failed request. Add an alert so you learn quickly when the job stops working.

Keep publishing a human decision

Creating drafts and letting a person press publish is a good default. It lets the automation handle the tedious setup while an editor checks the result. If you later schedule posts, use the future status with a date, and review the queue regularly.

Also avoid creating duplicates. Before creating a post, search for one with the same slug, or record the ID of each created post in your source file, so a re-run updates instead of duplicating.

Your next step

Generate an application password for a test user on a staging or test site. Use curl or a request tool to create a draft with a title and a short body, and confirm it appears in the editor.

Then add a category by ID and a featured image by media ID. Once all three work, turn the request into a workflow step that reads your drafts from a file or sheet.