Headless WordPress + Next.js Is A Game Changer
๐ **Source:** [Headless WordPress + Next.js Is A Game Changer](https://www.youtube.com/watch?v=2BUZUjtz5sM)
> [!note]+ Document Notes
> tags: [headless-cms, nextjs, wordpress]
> author: ByteGrad
> source: YouTube
> reference: https://www.youtube.com/watch?v=2BUZUjtz5sM
> date created: 2024-12-28 15:45
>
> # ๐ท๏ธ Title: Headless WordPress + Next.js Is A Game Changer
>
<!-- deep-read:sections start -->
## ๐งฐ Action Inbox
- [H1] Set up WordPress REST API endpoint exclusion from caching โ avoid stale data during development #wordpress
- [H2] Implement ISR with revalidate for dynamic content โ balance performance with freshness #nextjs
- [H3] Configure generate static params for dynamic routes โ enable static generation at build time #performance
## ๐งญ Table of Contents
- [๐ Summary](#-summary)
- [๐ Main Content](#-main-content)
- [๐๏ธ Notes](#๏ธ-notes)
- [๐ References](#-references)
- [๐ฃ๏ธ Paraphrase](#๏ธ-paraphrase)
- [๐ฐ Newsletter](#-newsletter)
- [โ
Action Items](#-action-items)
- [๐งช Hypotheses](#-hypotheses)
- [๐ Methodology](#-methodology)
- [โ Questions](#-questions)
- [๐ฎ Future Research](#-future-research)
- [๐ก Implications](#-implications)
- [๐ง Learning Styles Assessment](#-learning-styles-assessment)
- [๐ Accessibility Assessment](#-accessibility-assessment)
- [๐ DEIB Assessment](#-deib-assessment)
- [โ
Logical Validity](#-logical-validity)
- [๐ก๏ธ DARVO Analysis](#-darvo-analysis)
- [๐ฏ Conclusion](#-conclusion)
### ๐ References
- [WordPress REST API](https://developer.wordpress.org/rest-api/)
- [Next.js Documentation](https://nextjs.org/docs)
## ๐ Summary
- Tutorial demonstrates building a headless WordPress setup with Next.js frontend using REST API
- Covers fetching WordPress posts via REST API, handling caching strategies, and dynamic routing
- Shows practical implementation using Cloudways hosting with real-world caching considerations
- Addresses ISR (Incremental Static Regeneration) for balancing performance with content freshness
- Provides foundation for developers wanting modern frontend with familiar WordPress backend
## ๐ Main Content
The tutorial walks through creating a headless WordPress architecture where WordPress serves as a content management system accessed via its REST API, while Next.js handles the frontend presentation. This approach allows developers to leverage WordPress's familiar content management capabilities while using modern React-based frameworks for the user interface.
Key technical aspects include configuring WordPress to exclude API endpoints from server-level caching, implementing Next.js server components to fetch data, and setting up dynamic routes with proper static generation. The tutorial emphasizes practical caching strategies, including ISR for automatic content updates and generateStaticParams for build-time static generation.
The demonstration uses Cloudways hosting platform, showing real hosting considerations like cache configuration and multiple application deployment options. The approach is particularly valuable for teams already familiar with WordPress who want to modernize their frontend technology stack.
## ๐๏ธ Notes
#### โฑ๏ธ [00:00](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=0) โ [H1] Introduction to headless WordPress concept
- Key idea: WordPress can serve as headless CMS for modern frontend frameworks
- Evidence: "you can pull in data into your app using the WordPress REST API that comes with WordPress by default"
#### โฑ๏ธ [01:30](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=90) โ [H2] Setting up new Next.js project
- Key idea: Starting from scratch with Next.js 16 boilerplate
- Evidence: "Brand new NextJS16 as of recording"
#### โฑ๏ธ [02:45](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=165) โ [H2] WordPress hosting setup with Cloudways
- Key idea: Need WordPress instance accessible via REST API
- Evidence: "Cloudways is actually a digital ocean company, but they allow you to pick from these other providers as well"
#### โฑ๏ธ [04:20](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=260) โ [H3] Creating sample WordPress content
- Key idea: Default WordPress installation provides sample posts for testing
- Evidence: "by default there is this hello world post. Let's just add another post here"
#### โฑ๏ธ [05:15](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=315) โ [H1] Accessing WordPress REST API endpoints
- Key idea: WordPress automatically exposes REST API at /wp-json/wp/v2/posts
- Evidence: "if you use the URL for your site...and then if you tag on this bit here...I actually have a bunch of data here"
#### โฑ๏ธ [06:30](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=390) โ [H2] Fetching WordPress data in Next.js
- Key idea: Server components can fetch REST API data directly
- Evidence: "this is a server component. We can fetch data directly in here"
#### โฑ๏ธ [08:00](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=480) โ [H3] Displaying WordPress posts in React components
- Key idea: Map over fetched posts to render dynamic content
- Evidence: "just map over each of these posts...you can see now I have second post"
#### โฑ๏ธ [09:45](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=585) โ [H2] WordPress server-level caching issues
- Key idea: WordPress caching can prevent API updates from appearing
- Evidence: "There's also caching at the WordPress instance level...we may just want to exclude the API URL from that caching"
#### โฑ๏ธ [11:20](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=680) โ [H3] REST API optimization techniques
- Key idea: Use query parameters to optimize API responses
- Evidence: "_embed here...gives you additional information...reduce the payload as well as the speed"
#### โฑ๏ธ [12:30](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=750) โ [H3] Built-in WordPress REST API features
- Key idea: WordPress REST API includes search, filtering, and ordering
- Evidence: "if you have a search functionality, there is actually a built-in search feature...you can also specify the order by"
#### โฑ๏ธ [13:45](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=825) โ [H2] Creating dynamic routes for individual posts
- Key idea: Use Next.js dynamic routing with WordPress post slugs
- Evidence: "You can do it with route with square brackets...the slug will be in here in the URL"
#### โฑ๏ธ [15:30](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=930) โ [H1] Next.js caching strategies for WordPress content
- Key idea: Static generation provides performance but needs revalidation strategy
- Evidence: "the homepage is statically rendered...the HTML for that route is created and that will just be cached on a CDN"
#### โฑ๏ธ [17:00](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=1020) โ [H2] Implementing ISR with revalidate
- Key idea: ISR balances performance with content freshness
- Evidence: "this page will be cached for 1 minute...this is like ISR incremental static regeneration"
#### โฑ๏ธ [19:15](https://www.youtube.com/watch?v=2BUZUjtz5sM&t=1155) โ [H2] Using generateStaticParams for dynamic routes
- Key idea: Pre-generate static pages for all WordPress posts at build time
- Evidence: "generate static params function...specify for all possible slugs for which we want to generate a page during the build"
## ๐ References
- [WordPress REST API Documentation](https://developer.wordpress.org/rest-api/)
- [Next.js ISR Documentation](https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration)
- [Cloudways Hosting Platform](https://www.cloudways.com/)
## ๐ฃ๏ธ Paraphrase
- WordPress can function as a backend content management system while Next.js handles the frontend, connected through REST API calls
- Caching strategies must be coordinated between WordPress server-level caching and Next.js application-level caching for optimal performance
- Dynamic routing in Next.js combined with generateStaticParams enables static generation of all WordPress posts at build time
## ๐ฐ Newsletter
**Headless WordPress + Next.js: Modern Content Management Made Simple**
Discover how to combine WordPress's familiar content management with Next.js's modern frontend capabilities. This approach lets your content team continue using WordPress while developers build with React-based tools.
**Key Benefits:**
- Keep existing WordPress content workflows
- Gain modern frontend performance and flexibility
- Use WordPress REST API for seamless data fetching
- Implement smart caching with ISR for fresh content
**Getting Started:**
Set up WordPress with REST API access, create a Next.js app, and fetch content via standard HTTP requests. Configure caching strategies to balance performance with content freshness.
**Pro Tip:** Exclude WordPress API endpoints from server-level caching during development to see content changes immediately.
Resources: [WordPress REST API](https://developer.wordpress.org/rest-api/), [Next.js Documentation](https://nextjs.org/docs), [Learn WordPress](https://learn.wordpress.org/)
## โ
Action Items
#### DevRel
- Developer education: Create workshops on headless WordPress architecture patterns
- Documentation: Develop REST API integration guides for popular frontend frameworks
- Developer experience: Build starter templates for common headless WordPress setups
#### WordPress
- Core: Improve REST API performance and caching documentation
- Documentation: Expand headless WordPress guidance on DevHub
- Community: Support headless WordPress meetup presentations and case studies
#### Community
- Contributors: Guide headless WordPress plugin development best practices
- Ecosystem: Foster headless WordPress ecosystem coordination between WordPress and JavaScript communities
## ๐งช Hypotheses
- Headless WordPress adoption increases when developers can maintain existing content management workflows
- ISR implementation reduces server load while maintaining content freshness for most use cases
- GraphQL vs REST API choice depends more on developer familiarity than technical requirements
- Hosting platform caching configuration significantly impacts headless WordPress performance
### ๐ Methodology
- Performance testing comparing traditional vs headless WordPress implementations
- Developer survey on headless WordPress adoption barriers and motivations
- Cache hit ratio analysis across different revalidation strategies
### โ Questions
- How does headless WordPress performance compare to traditional WordPress with modern caching?
- What are the optimal revalidation intervals for different content types and update frequencies?
- How can WordPress admin UI be enhanced specifically for headless use cases?
### ๐ฎ Future Research
- WordPress admin interface optimizations for headless workflows
- Performance benchmarking of different headless architecture patterns
- Integration patterns with modern deployment platforms and CDNs
### ๐ก Implications
- Practice: Headless WordPress enables modern frontend development while preserving content management familiarity
- Education: Developers need training on both WordPress REST API and modern frontend caching strategies
- Security: API endpoint security becomes critical when WordPress serves as headless backend
- Performance: Caching coordination between WordPress and frontend frameworks requires careful configuration
### ๐ง Learning Styles Assessment
Reading level estimate: Grade 10-12
#### ๐ Visual learners
- Present: Code examples, terminal output, browser screenshots
- Improve: Architecture diagrams showing WordPress-Next.js data flow, caching visualization
#### ๐ Aural learners
- Present: Video tutorial format with narrated explanations
- Improve: Audio summary of key technical concepts and decision points
#### ๐ฃ๏ธ Verbal learners
- Present: Clear technical terminology definitions, step-by-step explanations
- Improve: Analogies for caching concepts, comparison with traditional WordPress
#### ๐งฉ Kinesthetic learners
- Present: Complete hands-on tutorial with working code examples
- Improve: Interactive coding exercises, troubleshooting checklists
#### ๐ง Logical learners
- Present: Sequential build process, clear cause-and-effect for caching decisions
- Improve: Decision trees for caching strategies, performance metrics
#### ๐งโ๐คโ๐ง Social learners
- Present: Implicit community discussion through tutorial format
- Improve: Discussion prompts, community examples, team workflow considerations
#### ๐งโโ๏ธ Solitary learners
- Present: Self-contained tutorial with individual project setup
- Improve: Self-assessment questions, independent exploration suggestions
#### ๐ฟ Naturalistic learners
- Present: Real-world hosting platform integration
- Improve: Ecosystem thinking about WordPress and JavaScript communities
### ๐ Accessibility Assessment
#### Screen reader semantics
- Present: Clear heading structure in tutorial, descriptive link text
- Improve: Alt text for code screenshots, ARIA labels for interactive elements
#### Keyboard navigation
- Present: Standard video controls accessible via keyboard
- Improve: Keyboard shortcuts for code sections, skip links for tutorial sections
#### Contrast and readability
- Present: High contrast code examples, readable font sizes
- Improve: Dark mode code examples, increased line spacing
#### Text resizing and reflow
- Present: Responsive video player and text content
- Improve: Ensure code examples remain readable at 200% zoom
#### Media and captions
- Present: Video tutorial format
- Improve: Closed captions, downloadable transcript, audio descriptions
#### Forms and interactions
- Present: Standard form elements in examples
- Improve: Form validation examples, accessible error handling
### ๐ DEIB Assessment
#### Cultural breadth
- Present: Global hosting platform options mentioned
- Improve: Multi-region deployment considerations, internationalization examples
#### Inclusive language
- Present: Gender-neutral technical language, defined jargon
- Improve: Plain language summaries, avoid framework-specific idioms
#### Neurodiversity support
- Present: Structured tutorial format, consistent code patterns
- Improve: Content warnings for rapid screen changes, summary sections
#### Representation
- Present: Technical content focuses on code and concepts
- Improve: Diverse example use cases, varied business contexts
#### Psychological safety
- Present: Encouraging tone about learning complex topics
- Improve: Explicit acknowledgment of learning curve, beginner-friendly alternatives
### โ
Logical Validity
#### Strong reasoning
- Deductive strength: Clear technical cause-and-effect for caching decisions
- Inductive strength: Generalizes from specific Next.js example to other frameworks
- Causal reasoning: Explains why headless architecture provides specific benefits
- Analogical strength: Implicit comparison to traditional WordPress architecture
#### Fallacies
- Appeal to novelty: Slight bias toward "new shiny framework" without discussing tradeoffs
- Hasty generalization: Claims about caching needs may not apply to all use cases
#### Tone of voice
- Overall tone: Educational and encouraging, with practical focus on implementation
### ๐ก๏ธ DARVO Analysis
- Deny: No denials or minimization present
- Attack: No attacks on accusers present
- Reverse victim/offender: No victim/offender dynamics present
- Emotional cues and rhetoric: Positive technical education with enthusiasm for modern development
## ๐ฏ Conclusion
Headless WordPress with Next.js provides a powerful architecture for teams wanting to modernize their frontend while maintaining familiar content management workflows. The key technical considerations involve coordinating caching strategies between WordPress and Next.js, implementing appropriate revalidation intervals, and configuring hosting platforms for API performance. This approach enables developers to leverage WordPress's mature content management capabilities while building with modern React-based frameworks and deployment patterns.
## ๐งฑ Refactor Appendix
### ๐ Literature Split Pad
Purpose: Stage CEE units that become Literature notes.
Split level: H3
### [H1] WordPress REST API enables headless CMS architecture
> [!quote] [Headless WordPress + Next.js Is A Game Changer](https://www.youtube.com/watch?v=2BUZUjtz5sM) by [[ByteGrad]] via [[YouTube]]. Published 2024. Accessed 2024-12-28.
> "you can pull in data into your app using the WordPress REST API that comes with WordPress by default"
Demonstrates practical implementation of headless WordPress using
---
## Full Transcript
[00:00] Hey everyone, in this video we're going<br>[00:01] to talk about how you can use your<br>[00:02] WordPress instance as like a headless<br>[00:05] CMS for a custom well next.js front end<br>[00:08] in this case, but you may use a<br>[00:10] different framework like React Vit or<br>[00:12] some other full stack framework and the<br>[00:14] fundamentals will stay the same. And<br>[00:16] those are that you can pull in data into<br>[00:18] your app using the WordPress REST API<br>[00:21] that comes with WordPress by default,<br>[00:23] but they actually also have a GraphQL<br>[00:25] option if you prefer that. So, in this<br>[00:27] video, I quickly want to go over how you<br>[00:28] can do that. Some tips and tricks, show<br>[00:30] you that it's a very realistic option.<br>[00:32] And I think this can be really helpful<br>[00:33] because maybe you are already familiar<br>[00:35] with WordPress or you have clients that<br>[00:37] want to stick to WordPress cuz they're<br>[00:38] already familiar with it, right? So, you<br>[00:40] may simply want to use WordPress, but<br>[00:41] you may want to use a more new shiny<br>[00:44] framework for the front end and that is<br>[00:46] totally doable. And I will explain how<br>[00:48] it works with uh caching as well, right?<br>[00:50] So, in XJS, we have a bunch of caching.<br>[00:52] So what you see here is actually an XJS<br>[00:54] application that has data here and that<br>[00:56] is coming from a separate WordPress<br>[00:59] instance. So how can we do that? Let's<br>[01:01] actually start from scratch, right? So<br>[01:02] I'm just going to ignore the styling in<br>[01:04] this video. You can style it like a blog<br>[01:06] or whatever your use case is. The<br>[01:08] essential thing is how can we get the<br>[01:09] data into the next JS app and how do we<br>[01:12] deal with caching like SSG and uh<br>[01:15] revalidation and ISR. I'll explain what<br>[01:18] those are. All right, so let's jump into<br>[01:20] it. So I have a completely new folder<br>[01:22] here. I have no app in here. I'm just<br>[01:25] going to create a new NexJS app. Brand<br>[01:28] new NexJS16 as of recording. All right.<br>[01:30] And let me actually run the dev server<br>[01:32] as well. And let's open it up. All<br>[01:34] right. So here we have the standard<br>[01:35] NextJS boiler plate. Let me remove this<br>[01:37] boiler plate here. So we just have an<br>[01:39] empty page. So I will just say hello<br>[01:41] here. All right. So now you can see I<br>[01:43] have one page here, the homepage, and<br>[01:44] it's just displaying hello text. But we<br>[01:47] want to display, let's say, posts from<br>[01:49] our WordPress instance. Now, we will<br>[01:50] need some WordPress instance. I'm going<br>[01:52] to use Cloudways in this video. Yes,<br>[01:54] they're also today's sponsor, but I had<br>[01:55] a great time using them. And as of<br>[01:57] recording, they actually have a great<br>[01:58] sale going on. So, check out the link in<br>[02:00] the description. And make sure to use my<br>[02:02] coupon code, which I will display here<br>[02:04] to get an even better price. So, if you<br>[02:06] want to follow along, go ahead and<br>[02:08] create a WordPress instance here with<br>[02:10] Cloudways. Really powerful hosting<br>[02:11] platform. And let me actually show you<br>[02:13] that. So I will go ahead and go here<br>[02:15] into the dashboard. The way it works is<br>[02:17] that we can buy a server and you can<br>[02:20] have multiple applications on one server<br>[02:22] but by default we can get started with<br>[02:24] one application. You can see it's<br>[02:25] optimized for all sorts of PHP<br>[02:27] applications. I will just go with a<br>[02:28] default WordPress installation. Here I<br>[02:31] can just say first app first server. For<br>[02:33] the application stack they actually have<br>[02:34] a lightning stack now which can be<br>[02:37] faster. And then we can pick the<br>[02:38] provider for the server. So, Cloudways<br>[02:40] is actually a digital ocean company, but<br>[02:42] they allow you to pick from these other<br>[02:44] providers as well, in case you do prefer<br>[02:46] that. We can pick the server type and we<br>[02:48] can pick how powerful it should be. And<br>[02:50] then we can pick the location. I<br>[02:52] recommend that you pick the location<br>[02:53] that is the closest to your actual<br>[02:55] users, not yourself, to your actual<br>[02:56] users. And then you can click on launch.<br>[02:58] All right. So, after a few minutes, it's<br>[03:00] good to go. We we have all the settings<br>[03:02] here for the server itself. But we want<br>[03:04] to see the actual applications on the<br>[03:06] server. So here you can see the actual<br>[03:08] details including access to the admin<br>[03:11] panel and actually let's go to the admin<br>[03:13] panel right here. I have my credentials<br>[03:15] right here automatically generated here<br>[03:17] by cloudways and I will log in right<br>[03:19] here. All right, here we go. Here we<br>[03:21] have our admin panel and here we have<br>[03:23] our site just a standard boil plate. If<br>[03:24] I go to posts now you can see by default<br>[03:26] there is this hello world post. Let's<br>[03:28] just add another post here. So we have<br>[03:30] two. I will call that second post. This<br>[03:32] is the second post. I will go ahead and<br>[03:35] publish that. Cool. So now if I go there<br>[03:37] on the front end that you get out of the<br>[03:39] box with WordPress, uh this is what we<br>[03:41] see. Now of course we want to have our<br>[03:43] own custom front end with Nex.js in this<br>[03:45] case. So how do we get these posts into<br>[03:47] our next JS app? Well by default<br>[03:49] actually you get a REST API. So if you<br>[03:52] use the URL for your site, this is the<br>[03:54] one I got from Cloudways. And then if<br>[03:56] you tag on this bit here and if I go<br>[03:58] there I actually have a bunch of data<br>[04:01] here. This is in so-called JSON format.<br>[04:04] I can click on preprint here to format<br>[04:06] it nicely. Now what I see here are<br>[04:08] actually those posts that we have. So<br>[04:11] you can see this piece here is one post,<br>[04:13] right? So here you can see title is<br>[04:15] second post. It has an ID and if we<br>[04:18] scroll down a little bit and here we can<br>[04:19] see the content, right? The actual this<br>[04:21] is the second post. We also see a slug<br>[04:23] here. So if you're building a blog and<br>[04:26] you want to have a slug in the URL, you<br>[04:28] can use this. So this is all the data<br>[04:30] about that one post. And then if I<br>[04:32] scroll down, there is another post here<br>[04:34] with a different slug and with its own<br>[04:36] data as well. So there are two posts<br>[04:38] here. Now, by the way, there's also an<br>[04:39] option here for pages instead of posts.<br>[04:42] So maybe that is something that you want<br>[04:44] to use. But in this video, we'll focus<br>[04:46] just on these posts here. So of course,<br>[04:48] I can go here directly in the URL. But<br>[04:50] how do we get this in the in my app here<br>[04:52] right here? Now, I should mention there<br>[04:53] are also plugins to use GraphQL instead<br>[04:55] of the REST API. However, in this video,<br>[04:57] we'll just use this. Okay, so now how do<br>[04:59] I get that data into my actual NexJS<br>[05:01] app? Well, here in the page component, I<br>[05:03] can just make a fetch call, right? This<br>[05:05] is a server component. We can fetch data<br>[05:07] directly in here and we just have to use<br>[05:08] that URL that we just used in the URL<br>[05:11] address bar. So, it's my Cloudways URL<br>[05:13] and then it's that piece at the end. I'm<br>[05:15] just parsing that and that's JSON data.<br>[05:17] So, I'm parsing that here and then I'm<br>[05:20] just logging it here, not displaying it<br>[05:21] on the page yet. Now when I do that you<br>[05:23] will see the you you can see here the<br>[05:26] log here in my terminal because it runs<br>[05:28] on the server right server component but<br>[05:30] you can see we get the same data here<br>[05:32] that we just saw in the browser as well.<br>[05:34] So now we can just display that here on<br>[05:35] the page right so I can have like an H1<br>[05:38] and then just map over each of these<br>[05:40] posts. Now if I go to my homepage you<br>[05:43] can see I have blog post and then I have<br>[05:45] second post and let me actually show it<br>[05:47] like this. So you can see now I have<br>[05:48] second post that's the<br>[05:50] post.title.rendered rendered and then we<br>[05:52] are also displaying the content and this<br>[05:54] is the second post and then we have the<br>[05:55] other post with this title and content<br>[05:58] and let me actually remove the H1 so we<br>[05:59] just have those post and let me actually<br>[06:01] add a border to the bottom of each post<br>[06:04] here right so now I've been able to get<br>[06:06] data from my WordPress instance<br>[06:07] basically use it as like a headless<br>[06:09] WordPress right so now I can manage my<br>[06:11] content from this dashboard pull it into<br>[06:14] my next JS app so if I make a change<br>[06:16] here and I add a bunch of exclamation<br>[06:18] marks and I save here so post update did<br>[06:21] it. All right. Now, when you make a<br>[06:22] change and you don't see it reflected in<br>[06:24] the app, there may actually be an issue<br>[06:25] with caching. We'll talk more about this<br>[06:26] later, but basically there is caching at<br>[06:29] the Nex.js app level, that's actually<br>[06:31] not the issue that I ran into. There's<br>[06:33] also caching at the WordPress instance<br>[06:35] level, right? So, typically this is<br>[06:37] actually really beneficial because it's<br>[06:39] going to be much faster. So, we we<br>[06:41] typically do want it. However, we may<br>[06:43] just want to exclude the API URL from<br>[06:47] that caching. Right? So, here I have the<br>[06:48] Breeze plugin. I can exclude a URL. So<br>[06:52] everything here with WordPress JSON, I'm<br>[06:54] just going to exclude from here. So that<br>[06:56] if I make a change here and if I save<br>[06:58] here and now if I refresh, you can see<br>[07:02] it's updated here. There are a couple of<br>[07:03] tricks by the way that we can do with<br>[07:04] the URL as well. So I can add this<br>[07:07] underscore embed here. And if I then log<br>[07:09] the data that we get, it will actually<br>[07:11] give you additional information that you<br>[07:13] perhaps would have had to make an<br>[07:15] additional request for. It comes now<br>[07:17] included as part of this payload. for<br>[07:19] example, an image URL or author<br>[07:22] information. You can also specify the<br>[07:24] exact fields that you want. So if I do<br>[07:26] this, I only get the ID, slug, title,<br>[07:28] and the content here. This may reduce<br>[07:30] the payload as well as the speed. Now,<br>[07:32] if you have many posts and you have like<br>[07:34] pagionation, there are some settings for<br>[07:36] that here as well. And actually, if you<br>[07:38] have a search functionality, there is<br>[07:40] actually a built-in search feature here<br>[07:41] as well. So you can see if I'm searching<br>[07:43] for the word second, it only gives me<br>[07:45] that post, that keyword. So the second<br>[07:48] post in this case you can also specify<br>[07:50] the order by. So if you want to have it<br>[07:52] sorted in a particular that is possible<br>[07:55] and there are also options for<br>[07:56] filtering. So for example only the post<br>[07:58] in a particular category with a certain<br>[08:00] tag. Now if you're building a blog we<br>[08:02] can use that built-in slug property. So<br>[08:05] we can just use the nextjs link<br>[08:07] component and then uh make it /ost the<br>[08:10] slug. So instead of displaying the<br>[08:12] content here we're going to put it on<br>[08:13] its own page. So I have created a new<br>[08:15] page here in XJS. You can do it with<br>[08:16] route with square brackets. So then when<br>[08:19] you click on one of them the slug will<br>[08:21] be in here in the URL. So we need to<br>[08:22] grab the slug from the URL. We can do<br>[08:24] that with params. We can get we can get<br>[08:26] the slug like this. And then we need to<br>[08:28] fetch the that individual post's data.<br>[08:31] So we can do that with slug is and then<br>[08:34] the slug from the URL. Right? So then we<br>[08:36] can render its title and content very<br>[08:38] similarly. Right? Just like before, but<br>[08:40] now it's on its own page. So now if I go<br>[08:42] to this one, you can see I get the data<br>[08:44] just for that one post. All right. Now<br>[08:46] what about caching everyone's favorite<br>[08:48] topic. Uh so we just saw that there is<br>[08:50] actually already a cache at the server<br>[08:53] level. So the WordPress instance<br>[08:55] depending on where you host it in some<br>[08:56] settings may already have some cache. So<br>[08:59] if you're still prototyping or<br>[09:00] developing your app, it's typically best<br>[09:02] I think to exclude it from the cache or<br>[09:04] have like separate environments. But<br>[09:06] basically as you saw on the server<br>[09:07] level, I excluded the WordPress API URL<br>[09:10] from any caching. Now of course we do<br>[09:12] want to have some caching at the next.js<br>[09:14] level. Now at the next s level how it<br>[09:16] does it work here with caching. If I run<br>[09:18] a build you will see some information<br>[09:21] about the pages. Right. So you can see<br>[09:23] the homepage is statically rendered.<br>[09:26] Right? So during the build the HTML for<br>[09:28] that route is created and that will just<br>[09:31] be cached on a CDN. Right? So it's going<br>[09:33] to be very fast. But what happens if you<br>[09:34] do add another post in your WordPress<br>[09:36] instance? that homepage does need to be<br>[09:39] well recreated you could say right so<br>[09:41] because then there's a new post so we<br>[09:42] need to create a new list here new a new<br>[09:44] page with a new list so next has some<br>[09:46] options that we can use so I can specify<br>[09:50] revalidate here should be 60 seconds so<br>[09:52] this so basically 1 minute so this means<br>[09:55] that this page will be cached for 1<br>[09:59] minute so if you quickly add another<br>[10:01] post in WordPress it's not immediately<br>[10:04] recreated but after a minute or how it<br>[10:07] will be recreated with the latest post.<br>[10:09] So you will have a little bit of time<br>[10:11] where it's out of date but with a block<br>[10:13] like this that doesn't really matter. So<br>[10:15] you can specify that here or in this<br>[10:16] case I can actually also specify it here<br>[10:18] in the fetch I can say next revalidate<br>[10:21] in 60 seconds. So then it also knows<br>[10:23] that this page needs to be regenerated<br>[10:27] after uh every minute or so, right? But<br>[10:30] if I run a build now, so now if I run a<br>[10:32] build, you can see the page is still<br>[10:33] static, right? So it's it's still going<br>[10:35] to be very fast, right? But at some<br>[10:36] point we will have new data. So it needs<br>[10:38] to be fresh again. So it will be<br>[10:40] revalidated every 1 minute. And so this<br>[10:42] is like ISR incremental static<br>[10:44] regeneration. And so with this refund<br>[10:46] date option, we basically accept there's<br>[10:48] going to be a small, you know, period of<br>[10:50] time where it may be out of date. If you<br>[10:52] want it to be immediately updated when<br>[10:55] you make a change, we could create an<br>[10:57] API endpoint, a route handler, and then<br>[10:59] you need to send a web hook to your<br>[11:01] next.js app, then in that web hook, you<br>[11:03] can call validate path to trigger<br>[11:06] recreation of that page. But for a blog,<br>[11:08] this typically makes more sense. Now,<br>[11:10] what about this other page? This is a<br>[11:12] dynamic page. So during the build there<br>[11:15] is no caching. So the reason it's<br>[11:16] dynamic is because nextjs during build<br>[11:18] time does not know all the possible<br>[11:20] slugs that there could be. So it doesn't<br>[11:22] know for which slugs it needs to<br>[11:23] generate this page. Right? Because this<br>[11:25] page depends on that. There is a<br>[11:27] generate static params function here.<br>[11:29] And we can then specify for all possible<br>[11:32] slugs for which we want to generate a<br>[11:34] page during the build. So I can actually<br>[11:36] just fetch just the slugs of my posts in<br>[11:39] my WordPress. And that's what I'm going<br>[11:41] to return here. So basically an array of<br>[11:42] slugs. So then if I run another build,<br>[11:45] you can see now it has statically<br>[11:47] generated two pages here thanks to this<br>[11:50] option that you have as well. So this<br>[11:52] function runs only during build time. So<br>[11:54] I don't want to add caching here. But I<br>[11:56] could specify the revalidate here as<br>[11:58] well. So if I change the title or<br>[12:00] something else about this blog post,<br>[12:02] this is reflected at some point in my<br>[12:04] application as well. But if the caching<br>[12:06] is not completely clear to you, that's<br>[12:07] totally fine. It's one of the most<br>[12:09] complex topics in Next.js. I will have a<br>[12:11] new video soon that will give you a good<br>[12:13] explanation. Now, in the meantime, if<br>[12:15] you want to spin up a WordPress<br>[12:17] instance, I highly recommend you check<br>[12:18] out Cloudways with the link in the<br>[12:20] description and use my coupon code.<br>[12:22] Right? So, really powerful platform. I<br>[12:24] just have one application on my server<br>[12:26] right now, but I could have multiple<br>[12:27] applications, not just WordPress. If<br>[12:29] you're in if you if you have a PHP<br>[12:31] application or Laravel, something like<br>[12:33] that, you can host that on that server.<br>[12:35] They also have an autonomous option here<br>[12:37] with autoscaling as well. It's built on<br>[12:39] Kubernetes. So, if you're looking for a<br>[12:41] scalable WordPress hosting solution,<br>[12:43] check out Cloudways. In any case, I hope<br>[12:45] this helps you out. Thank you for<br>[12:47] watching and I hope to see you in the<br>[12:48] next one.
<!-- deep-read:sections end -->
## ๐บ Video

---
# ๐ Full Transcript
*Auto-generated from YouTube captions ยท timestamps roughly every 30 seconds ยท [watch on YouTube](https://www.youtube.com/watch?v=2BUZUjtz5sM)*
**[00:00]** Hey everyone, in this video we're going to talk about how you can use your WordPress instance as like a headless CMS for a custom well next.js front end in this case, but you may use a different framework like React Vit or some other full stack framework and the fundamentals will stay the same. And those are that you can pull in data into your app using the WordPress REST API that comes with WordPress by default, but they actually also have a GraphQL option if you prefer that. So, in this video, I quickly want to go over how you can do that. Some tips and tricks, show
**[00:30]** you that it's a very realistic option. And I think this can be really helpful because maybe you are already familiar with WordPress or you have clients that want to stick to WordPress cuz they're already familiar with it, right? So, you may simply want to use WordPress, but you may want to use a more new shiny framework for the front end and that is totally doable. And I will explain how it works with uh caching as well, right? So, in XJS, we have a bunch of caching. So what you see here is actually an XJS application that has data here and that is coming from a separate WordPress instance. So how can we do that? Let's
**[01:01]** actually start from scratch, right? So I'm just going to ignore the styling in this video. You can style it like a blog or whatever your use case is. The essential thing is how can we get the data into the next JS app and how do we deal with caching like SSG and uh revalidation and ISR. I'll explain what those are. All right, so let's jump into it. So I have a completely new folder here. I have no app in here. I'm just going to create a new NexJS app. Brand new NexJS16 as of recording. All right. And let me actually run the dev server
**[01:32]** as well. And let's open it up. All right. So here we have the standard NextJS boiler plate. Let me remove this boiler plate here. So we just have an empty page. So I will just say hello here. All right. So now you can see I have one page here, the homepage, and it's just displaying hello text. But we want to display, let's say, posts from our WordPress instance. Now, we will need some WordPress instance. I'm going to use Cloudways in this video. Yes, they're also today's sponsor, but I had a great time using them. And as of recording, they actually have a great sale going on. So, check out the link in the description. And make sure to use my coupon code, which I will display here
**[02:04]** to get an even better price. So, if you want to follow along, go ahead and create a WordPress instance here with Cloudways. Really powerful hosting platform. And let me actually show you that. So I will go ahead and go here into the dashboard. The way it works is that we can buy a server and you can have multiple applications on one server but by default we can get started with one application. You can see it's optimized for all sorts of PHP applications. I will just go with a default WordPress installation. Here I can just say first app first server. For the application stack they actually have
**[02:34]** a lightning stack now which can be faster. And then we can pick the provider for the server. So, Cloudways is actually a digital ocean company, but they allow you to pick from these other providers as well, in case you do prefer that. We can pick the server type and we can pick how powerful it should be. And then we can pick the location. I recommend that you pick the location that is the closest to your actual users, not yourself, to your actual users. And then you can click on launch. All right. So, after a few minutes, it's good to go. We we have all the settings here for the server itself. But we want
**[03:04]** to see the actual applications on the server. So here you can see the actual details including access to the admin panel and actually let's go to the admin panel right here. I have my credentials right here automatically generated here by cloudways and I will log in right here. All right, here we go. Here we have our admin panel and here we have our site just a standard boil plate. If I go to posts now you can see by default there is this hello world post. Let's just add another post here. So we have two. I will call that second post. This is the second post. I will go ahead and
**[03:35]** publish that. Cool. So now if I go there on the front end that you get out of the box with WordPress, uh this is what we see. Now of course we want to have our own custom front end with Nex.js in this case. So how do we get these posts into our next JS app? Well by default actually you get a REST API. So if you use the URL for your site, this is the one I got from Cloudways. And then if you tag on this bit here and if I go there I actually have a bunch of data here. This is in so-called JSON format. I can click on preprint here to format
**[04:06]** it nicely. Now what I see here are actually those posts that we have. So you can see this piece here is one post, right? So here you can see title is second post. It has an ID and if we scroll down a little bit and here we can see the content, right? The actual this is the second post. We also see a slug here. So if you're building a blog and you want to have a slug in the URL, you can use this. So this is all the data about that one post. And then if I scroll down, there is another post here with a different slug and with its own
**[04:36]** data as well. So there are two posts here. Now, by the way, there's also an option here for pages instead of posts. So maybe that is something that you want to use. But in this video, we'll focus just on these posts here. So of course, I can go here directly in the URL. But how do we get this in the in my app here right here? Now, I should mention there are also plugins to use GraphQL instead of the REST API. However, in this video, we'll just use this. Okay, so now how do I get that data into my actual NexJS app? Well, here in the page component, I can just make a fetch call, right? This is a server component. We can fetch data
**[05:07]** directly in here and we just have to use that URL that we just used in the URL address bar. So, it's my Cloudways URL and then it's that piece at the end. I'm just parsing that and that's JSON data. So, I'm parsing that here and then I'm just logging it here, not displaying it on the page yet. Now when I do that you will see the you you can see here the log here in my terminal because it runs on the server right server component but you can see we get the same data here that we just saw in the browser as well. So now we can just display that here on the page right so I can have like an H1
**[05:38]** and then just map over each of these posts. Now if I go to my homepage you can see I have blog post and then I have second post and let me actually show it like this. So you can see now I have second post that's the post.title.rendered rendered and then we are also displaying the content and this is the second post and then we have the other post with this title and content and let me actually remove the H1 so we just have those post and let me actually add a border to the bottom of each post here right so now I've been able to get data from my WordPress instance basically use it as like a headless
**[06:09]** WordPress right so now I can manage my content from this dashboard pull it into my next JS app so if I make a change here and I add a bunch of exclamation marks and I save here so post update did it. All right. Now, when you make a change and you don't see it reflected in the app, there may actually be an issue with caching. We'll talk more about this later, but basically there is caching at the Nex.js app level, that's actually not the issue that I ran into. There's also caching at the WordPress instance level, right? So, typically this is actually really beneficial because it's going to be much faster. So, we we
**[06:41]** typically do want it. However, we may just want to exclude the API URL from that caching. Right? So, here I have the Breeze plugin. I can exclude a URL. So everything here with WordPress JSON, I'm just going to exclude from here. So that if I make a change here and if I save here and now if I refresh, you can see it's updated here. There are a couple of tricks by the way that we can do with the URL as well. So I can add this underscore embed here. And if I then log the data that we get, it will actually
**[07:11]** give you additional information that you perhaps would have had to make an additional request for. It comes now included as part of this payload. for example, an image URL or author information. You can also specify the exact fields that you want. So if I do this, I only get the ID, slug, title, and the content here. This may reduce the payload as well as the speed. Now, if you have many posts and you have like pagionation, there are some settings for that here as well. And actually, if you have a search functionality, there is actually a built-in search feature here
**[07:41]** as well. So you can see if I'm searching for the word second, it only gives me that post, that keyword. So the second post in this case you can also specify the order by. So if you want to have it sorted in a particular that is possible and there are also options for filtering. So for example only the post in a particular category with a certain tag. Now if you're building a blog we can use that built-in slug property. So we can just use the nextjs link component and then uh make it /ost the slug. So instead of displaying the
**[08:12]** content here we're going to put it on its own page. So I have created a new page here in XJS. You can do it with route with square brackets. So then when you click on one of them the slug will be in here in the URL. So we need to grab the slug from the URL. We can do that with params. We can get we can get the slug like this. And then we need to fetch the that individual post's data. So we can do that with slug is and then the slug from the URL. Right? So then we can render its title and content very similarly. Right? Just like before, but now it's on its own page. So now if I go
**[08:42]** to this one, you can see I get the data just for that one post. All right. Now what about caching everyone's favorite topic. Uh so we just saw that there is actually already a cache at the server level. So the WordPress instance depending on where you host it in some settings may already have some cache. So if you're still prototyping or developing your app, it's typically best I think to exclude it from the cache or have like separate environments. But basically as you saw on the server level, I excluded the WordPress API URL from any caching. Now of course we do want to have some caching at the next.js
**[09:14]** level. Now at the next s level how it does it work here with caching. If I run a build you will see some information about the pages. Right. So you can see the homepage is statically rendered. Right? So during the build the HTML for that route is created and that will just be cached on a CDN. Right? So it's going to be very fast. But what happens if you do add another post in your WordPress instance? that homepage does need to be well recreated you could say right so because then there's a new post so we need to create a new list here new a new
**[09:44]** page with a new list so next has some options that we can use so I can specify revalidate here should be 60 seconds so this so basically 1 minute so this means that this page will be cached for 1 minute so if you quickly add another post in WordPress it's not immediately recreated but after a minute or how it will be recreated with the latest post. So you will have a little bit of time where it's out of date but with a block like this that doesn't really matter. So
**[10:15]** you can specify that here or in this case I can actually also specify it here in the fetch I can say next revalidate in 60 seconds. So then it also knows that this page needs to be regenerated after uh every minute or so, right? But if I run a build now, so now if I run a build, you can see the page is still static, right? So it's it's still going to be very fast, right? But at some point we will have new data. So it needs to be fresh again. So it will be revalidated every 1 minute. And so this is like ISR incremental static regeneration. And so with this refund
**[10:46]** date option, we basically accept there's going to be a small, you know, period of time where it may be out of date. If you want it to be immediately updated when you make a change, we could create an API endpoint, a route handler, and then you need to send a web hook to your next.js app, then in that web hook, you can call validate path to trigger recreation of that page. But for a blog, this typically makes more sense. Now, what about this other page? This is a dynamic page. So during the build there is no caching. So the reason it's
**[11:16]** dynamic is because nextjs during build time does not know all the possible slugs that there could be. So it doesn't know for which slugs it needs to generate this page. Right? Because this page depends on that. There is a generate static params function here. And we can then specify for all possible slugs for which we want to generate a page during the build. So I can actually just fetch just the slugs of my posts in my WordPress. And that's what I'm going to return here. So basically an array of slugs. So then if I run another build, you can see now it has statically
**[11:47]** generated two pages here thanks to this option that you have as well. So this function runs only during build time. So I don't want to add caching here. But I could specify the revalidate here as well. So if I change the title or something else about this blog post, this is reflected at some point in my application as well. But if the caching is not completely clear to you, that's totally fine. It's one of the most complex topics in Next.js. I will have a new video soon that will give you a good explanation. Now, in the meantime, if you want to spin up a WordPress instance, I highly recommend you check
**[12:18]** out Cloudways with the link in the description and use my coupon code. Right? So, really powerful platform. I just have one application on my server right now, but I could have multiple applications, not just WordPress. If you're in if you if you have a PHP application or Laravel, something like that, you can host that on that server. They also have an autonomous option here with autoscaling as well. It's built on Kubernetes. So, if you're looking for a scalable WordPress hosting solution, check out Cloudways. In any case, I hope this helps you out. Thank you for watching and I hope to see you in the next one.