This is an example of utilizing the WP REST API to create an interactive application. Without logging into the wp-admin, you will be able to:
- Create a post
- Update a post
- Delete a post
- Display all posts
Since this is a front-end application, you would also need a WordPress installation to operate as the back-end/data-store. That is where all posts will be retrieved from.
Also, the plugin WP Basic Auth is required to allow you to authenticate via the frontend and perform POST, PUT and DELETE requests. So make sure that is installed to your WP site.
- Download the project files from this repo
- Change the following variables in the /assets/js/index.js file
const url = ...
in line 2. This should be the URL of your WP site.window.btoa('admin:password')
in line 111, 155 and 206. This should be the username and password of your WP site.
- Install and activate the WP Basic Auth plugin
- Fire up index.html in your browser
Enjoy RESTing!
If you have issues with the creating, updating and deleting of posts, here are some possible solutions:
- Logout of your WP site or open the App in an incognito window/different browser. This is an issue with the WP Basic Auth plugin. More info.
- Add the following to your .htaccess file, right below
RewriteEngine On
More info.RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
If you need help or want to report any bugs, please feel free to open up an issue!
Last tested with WP v5.2.3