Today we are going to learn how to create a WordPress theme using React, TypeScript, and Frontity. Additionally, we will add Vite underneath, which will allow us to deploy our app faster.
Frontity is the framework that allows you to connect your React application with WordPress, leveraging its REST API or GraphQL to fetch and display content.
You can install Frontity using npm (Node Package Manager), which is a tool for installing Node.js and JavaScript packages in general. Here are the steps to install Frontity:
- Install Node.js and npm: If you don’t have Node.js and npm installed on your system, you’ll need to download and install them. You can download them from the official Node.js website: https://nodejs.org/.
- Create a New Frontity Project: Once you have Node.js and npm installed, you can create a new Frontity project using the following command in your terminal:
npx frontity create my-theme --typescript
This will create a new Frontity project in a directory called my-theme
.
Now it asks us which theme template we want as a reference, in my case I will choose mars-theme:
Once installed, we will configure the project with Vite.
To do this, go to frontity.settings.ts and add:
"builder": { "type": "vite", },
Install Additional Dependencies: After creating the project, make sure to navigate to your theme directory (my-theme
) and run the following command to install all necessary dependencies:
cd my-theme npm install
Once you have completed these steps, you will have Frontity installed on your system and you can start developing your WordPress theme using React with Frontity and Vite as the development environment.
To test the template theme, run:
npm run dev
It will now run the test environment. If you want it to load content from your WordPress, you will need to put your WordPress URL in frontity.settings.ts:
{ "name": "@frontity/wp-source", "state": { "source": { "url": "https://www.yourWordPressURL.com" } } },
*Change www.yourWordPressURL.com to your WordPress URL