Prerequisites Before you begin, you should have the following:
- A WordPress website (self-hosted or on WordPress.com) with the WP REST API enabled.
- Node.js and npm installed on your local machine.
- Basic knowledge of React, Next.js, and WordPress.
First, you need to create a new Next.js app. Open your terminal and run the following commands:
npx create-next-app my-wordpress-site
cd my-wordpress-site
This will create a new Next.js app with the name my-wordpress-site.
Next, you need to install the required dependencies to work with WordPress. Run the following commands in your terminal:
npm install --save isomorphic-fetch
npm install --save node-fetch
npm install @wordpress/url --save
These packages will allow you to fetch data from your WordPress website.
Next, you need to create a WordPress API client to fetch data from your WordPress website. Create a new file in your project directory called lib/wordpress.js and paste the following code:
import fetch from 'isomorphic-fetch';
import { parse } from 'url';
import { toParams } from 'url';
const WORDPRESS_URL = process.env.WORDPRESS_URL;
export async function fetchAPI(path) {
const url = parse(WORDPRESS_URL);
url.pathname = `/wp-json/wp/v2/${path}`;
url.query = { ...url.query, _embed: true };
const res = await fetch(url.format());
const data = await res.json();
return data;
}
export async function fetchAllPosts() {
const params = toParams({ per_page: 100, page: 1 });
const data = await fetchAPI(`posts?${params}`);
return data;
}
export async function fetchPostBySlug(slug) {
const data = await fetchAPI(`posts?slug=${slug}`);
return data[0];
}
This code will allow you to fetch posts and other data from your WordPress website.
Next, you need to create a WordPress page template to display the data from your WordPress website. Create a new file in your project directory called pages/post/[slug].js and paste the following code:
import { fetchPostBySlug } from '../../lib/wordpress';
export default function Post({ post }) {
return (
<div>
<h1>{post.title.rendered}</h1>
<div dangerouslySetInnerHTML={{ __html: post.content.rendered }} />
</div>
);
}
export async function getServerSideProps({ params }) {
const post = await fetchPostBySlug(params.slug);
return { props: { post } };
}
This code will fetch the post data from your WordPress website and display it on a page.
Finally, you need to deploy your app. There are many ways to do this, but one common way is to use a platform like Vercel or Netlify. Simply connect your GitHub repository to the platform and configure your deployment settings.
Congratulations! You have now created a Next.js WordPress website and deployed it to the web. You can now customize your app to your liking and add more features.