This article will show you how to get started with development of SvelteKit front-end web application using headless WordPress as a CMS(Content Management System). We will also use GraphQL as our query language tool to retrieve data from wordpress.
Install Local By FlyWheel
Local By FlyWheel is a local web server that allows you to develop your website on your local machine instead of a staging or live server.
Open up the app and click the + icon in the lower-left corner
Type your name of site you want to build
Check preffered option and click continue
Type your wordpress username, password, email and click Add Site
If everything goes well, you will be able to see the following information regarding the configured local server.
Please click Admin button in the top right corner to open up wp-admin login page in the web browser.
Access wp-admin dashboard from web browser
Use your username and password that you've set above to log in to wordpress admin panel.
Create a couple of dummy posts in the wp-admin dashboard
Click Posts and Add New. Creating a post is very intuitive and make sure you also set featuredImage so that we can render an image for each post in the front-end application.
Install WPGraphQL plugin
Click Install button and then Activate button as well.
Turn WordPress backend as GraphQL Server using WPGraphQL plugin
Now you will be able to see GraphQL menu from the left panel. Hover your mouse over the GraphQL menu and click Settings:
You will be able to find your graphql endpoint:
In my case, it is http://sveltekitwordpressdemo.local/graphql. This endpoint will be used later in SvelteKit to make our HTTP POST request to send our graphql query and retreive data we want.
Now that we've set up everything we need in the local wordpress server, let's get started with creating a new SvelteKit project.
Set up a new SvelteKit project
Terminal
npm init svelte@next sveltekit-wordpress-demo
cd sveltekit-wordpress-demo
npminstall
npm run dev -- --open
Configure CSS and layout
Create a layout css file to globally apply css rules.
Create an environment variable for graphql endpoint
Create .env.local file in your project root directory and type VITE_PUBLIC_WORDPRESS_API_URL=. Then copy and paste the graphql endpoint that we've seen in the WPGraphQL Settings in the wp-admin panel.
Blog Page
Run a graphql query from GraphiQL IDE in the wordpress admin panel
When you work with graphql, a typical way to develop is:
Make a query in the GraphiQL IDE tool and make sure it returns a resulting data.
Then you will copy the query and use it as a query variable in the one of those Svelte files that will render a page in your web application.
Go back to wp-admin panel and open up GraphiQL IDE and click Query Composer. You can drill down posts item and select the items as they appear in the following screen capture. Once you selected items you want, you can click the play button to see the resulting data that we would like to want from our SvelteKit front-end application.
Create a svelte file to render a Blog page
Now let's create a svelte file to render our Blog page.
Create ./src/routes/blog/index.svelte file that will make a HTTP POST request to send a graphql query and receive a resulting blog posts data and then render a list of the blog post titles.
Now when you check the Blog page again, you should see the blog posts rendered in a card format.
Individual Post Page
However, we can't click an individual blog card to see the detail post yet. To be able to do that, we need to make a dynamic route that is going to be rendered depending on each slug of the blog post.
Develop a graphql query
You can run the following graphql query from the GraphiQL IDE in the wp-admin panel:
73 ✍️ {post.author.node.name} on {formatDate(post.date)}
74</p>
75<div>{@html post.content}</div>
76{#if categories.length}
77<divclass="category-list">
78<h4>Categorized As</h4>
79<p>{categories.join(', ')}</p>
80</div>
81{/if}
82</article>
83
84<style>
85.blog-link{
86text-decoration: none;
87}
88article{
89margin-top:2rem;
90}
91article img{
92max-width:100%;
93}
94.category-list{
95border-top:2px solid var(--color-gray-9);
96margin-top:2.5rem;
97padding-top:2rem;
98}
99.category-list h4{
100margin:0;
101}
102</style>
Test a Post page
Now that the dynamic routes are all set, we can click the individual post to see the detail contents:
Adding a Podcast Player
So far, we retrieved blog post data from wordpress graphql server and rendered in our SvelteKit front-end application. Because the SvelteKit is a SPA(Single Page Application), we can create a persistent component that can be rendered throught all the pages. For example, we can create a footer like component that plays a podcast, but you can still navigate to other pages without interrupting the playing podcast.
Comments