Astro + WordPress = πŸš€

Maciek Palmowski

I work at Kinsta

contact me hi@maciekpalmowski.dev

Headless

  • Decoupling back-end from front-end
  • Using WordPress just to serve data
  • Using this data we can do whatever we want with it

Headless vs monolith is like building Legos vs knitting

Why Astro?

  • Because few developers on Twitter says so
  • Because it's quite new and build with bleeding edge tools
  • It's Jamstack, so it has to be great

Seriously, why Astro?

  • Supports both SSG and SSR
  • Zero JavaScript Runtime
  • Island Architecture
  • Bring your own framework (or not)

Some basics

Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ Header.astro
β”‚   β”œβ”€β”€ layouts/
β”‚   β”‚   └── PostLayout.astro
β”‚   └── pages/
β”‚   β”‚   └── index.astro
β”‚   └── styles/
β”‚       └── global.css
β”œβ”€β”€ public/
β”‚   └── social-image.png
β”œβ”€β”€ astro.config.mjs
β”œβ”€β”€ package.json
							
							

Astro component

---
// Component Script (JavaScript)
---

							
							

Astro component

---
const name = "WordUp TrΓ³jmiasto";
---

Hello {name}!

Astro API component

---
const response = await fetch('https://randomuser.me/api/');
const data = await response.json();
const randomUser = data.results[0];
---

Hello {randomUser.name.first}

{randomUser.name.last}!

Astro Layouts

---
---
<html lang="en">
<head>
	
	My Cool Astro Site
</head>
<body>
	
	
<slot />
</body> </html>

Astro Layouts

---
import MyLayout from '../layouts/MyLayout.astro';
---
<MyLayout>
	

Hello WordUp.

</MyLayout>

Static Routing

src/pages/index.astro        -> mysite.com/
src/pages/about.astro        -> mysite.com/about
src/pages/test/about.astro   -> mysite.com/test/about
							

Dynamic Routing

pages/blog/[slug].astro β†’ (/blog/hello-world, /blog/post-2, etc.)
pages/[username]/settings.astro β†’ (/fred/settings, /drew/settings, etc.)
pages/[lang]-[version]/info.astro β†’ (/en-v1/info, /fr-v2/info, etc.)
							

WordPress time

Preparations

  • Make sure your WP has REST or GraphQL API enabled

Creating an .env file

PUBLIC_API_URL = https://domain.com/wp-json/wp/v2

Fetching data

const API_URL = import.meta.env.PUBLIC_API_URL;
const res = await fetch( `${API_URL}/articles?per_page=50` );
const data = res.json();

Archive

---
import Article from '../components/Article.astro';
...
const data = res.json();
---

Articles archive

    {data.map(post => <Article post={post}/>)}

Single pages

First we have to create a pages/[slug].astro file.

Single pages

---
export async function getStaticPaths() {
	const posts = await fetch(...);
	return posts.data.map((post) => {
		return {
			params: { slug: post.slug },
			props: { post }
		};
	});
}
const {post} = Astro.props;
---

{post.title.rendered}

GraphQL

const slug = 'about';
const response = await fetch(import.meta.env.WORDPRESS_API_URL, {
	method: 'POST',
	headers: { 'Content-Type': 'application/json' },
	body: `
	{
		page(id:"${slug}", idType:URI) {
		title
		content
		}
	}
	`
});
const data = await response.json();

Is it worth investing your time?

  • It depends

Any questions?

Thank you

  • Reach me out on Twitter - @palmiak_fp
  • Sign up to my newsletter - newsletter.maciekpalmowski.dev