So I recently installed WordPress using Docker, which is a straightforward fill-in-the-blank compose file from Docker docs (https://docs.docker.com/compose/wordpress/).
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD:
MYSQL_DATABASE:
MYSQL_USER:
MYSQL_PASSWORD:
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER:
WORDPRESS_DB_PASSWORD:
volumes:
db_data:
It's up-and-running. That part is fine, but when I head to http://siteurl/wp-json
I get a 404. Site works fine, but the REST API isn't accessible. I have another site running on WAMP and when I got to that address it pops out:
{
"name": "localhost:8090",
"description": "Just another WordPress site",
"url": "http://localhost:8090/wordpress",
"home": "http://localhost:8090/wordpress",
"gmt_offset": "0",
"timezone_string": "",
"namespaces": [
"oembed/1.0",
"wp/v2"
],
...
Both sites are running 4.8. How do I access the REST API when running WordPress on Docker? I usually develop locally using Docker and don't recall this being a problem.
(As a side note, I spun up an WordPress container that Bitnami publishes and had no problem getting the proper response. So this is an issue with the... official WordPress image? Maybe the underlying stack for the image?? I can use it, but I'd really, really, really like to know what the problem is because I've been seeing a similar issue crop up for my fellow devs)
Download the WordPress REST API Basic Auth plugin. Log in to your WordPress Dashboard and go to Plugins -> Add New. Click on the Upload Plugin button and select the plugin's zip file. Go to the Installed Plugins menu and activate the plugin from there.
Is the WordPress REST API enabled? The best way to check is to visit this URL: https://yoursite.com/wp-json. If you see some information which seems related to your WordPress REST API, it works. If you see something, it means that, at least, your WordPress REST API is enabled.
The WordPress REST API provides REST endpoints (URLs) representing the posts, pages, taxonomies, and other built-in WordPress data types. Your application can send and receive JSON data to these endpoints to query, modify and create content on your site.
As it turns out this has to do with the permalink setting for your site. The /wp-json/wp/v2
end point is available when you set your site is set up to use the custom permalink setting. If I use the /%post%/
permalink structure it works. There is an alternate route for sites that use other permalink structures:
On sites without pretty permalinks, the route is instead added to the URL as the rest_route parameter. For the above example, the full URL would then be http://example.com/?rest_route=/wp/v2/posts/123
Source: https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/
In fact the ?rest_route=/wp/v2/posts
appears to always work, making it the more reliable option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With