What Is Headless WordPress?
Being ‘Headless’ does not imply that the CMS does not have a header section. It means that you may utilize the CMS as a general content management system regardless of the front-end technology employed.
WordPress, as an open-source platform, provides a lot of freedom in terms of creating any type of website. Using Headless WordPress Themes as a headless CMS allows you to use any web technology to build the front-end of your online application and manage its content using one of the most popular CMS. You could find yourself in a position where you need to add a blog feature to an existing React or Angular web project. Instead of creating a full-fledged content management system in the local framework, utilize the Rest API to manage content with the headless WordPress CMS.
Download the WP-Rest API plugin’s zip file from git and place it in the plugins folder of your WordPress installation. Activate the plugin when the file has been uploaded. Within your WordPress website, this will enable the WordPress Rest API.
Go to Permalinks Settings and choose either Post name or Custom Structure. Because we’ll be working with API requests, install the Postman Chrome plugin.
Create custom post types using the Custom Post Type UI plugin. When we utilize WordPress with React in the following phase, this custom post will be shown. After you’ve installed and activated the plugin, go ahead and create a new post type. I’ll be making a new post type for Books in this tutorial. Type the name of your custom post in the Post Type Slug.
Make sure the Show in REST API option is checked, as well as the REST API base slug. If you want to utilize WordPress as a headless CMS, you’ll need to do this. Fill in all of the boxes for the data you want to obtain via the REST API. After you’ve saved your modifications, you’ll see that the sidebar has been updated with a new option. To add the new Book to your custom post type, click on it.
Try hitting the URL with Postman to see if the data is available through the API. The URL should now be something along the lines of https://exampe.com/wp-json/wp/v2/books.
Using ACF and ACF to RestAPI plugins, we can additionally add other fields like Publisher. Both plugins must be installed and activated. Because ACF does not communicate to the WordPress Rest API by default, we must also install the ACF REST API plugin. Within my custom post type, a new field has been added. That was all there was to it for setting up WordPress to transmit data to our ReactJS web app.
The following requirements must be installed on your computer to develop React applications.
- NodeJS & NPM.
- Text Editor such as Sublime or Visual Studio Code.
- Git for version controlling (Optional)
After you’ve set up the environment, enter the command prompt and type the following code to make a ReactJS project. cd (change directory) to the application folder and execute the following command to install the Axios package for API calls after the app has been developed.
Open the folder in your preferred text editor now. Visual Studio Code is what is using. Run the command npm start to start the application. If everything goes well, we’ll be able to develop our React web application utilizing WordPress as a headless CMS. Create a new folder called ‘components’ inside the src folder, and a new file called ‘Books.js’ inside the ‘components’ folder.
We’ll get the data via the WordPress Rest API in the Book.js file. The code that queries the Rest API end-point – in my instance, ‘/books’ – and shows the data in JSON format.
By putting the code within the render method, the data may be shown. We’ll construct a new component called BookItems.js instead of showing data here since I want to keep it distinct from the parent component.