
Rendering External API Data in WordPress Blocks on the Front End
While the block basics are nicely covered in that post, I want to take it another step forward. You see, in that article, we learned the difference between rendering blocks in the back-end WordPress Block Editor and rendering them on the front-end theme. The example was a simple Pullquote Block that rendered different content and styles on each end.
Let’s go further and look at using dynamic content in a WordPress block. More specifically, let’s fetch data from an external API and render it on the front end when a particular block is dropped into the Block Editor.
There’s more than one way to integrate an API with a WordPress block! Since the article on block basics has already walked through the process of making a block from scratch, we’re going to simplify things by using the @wordpress/create-block
package to bootstrap our work and structure our project.
First things first: let’s spin up a new project from the command line:
I normally would kick a project like this off by making the files from scratch, but kudos to the WordPress Core team for this handy utility!
Once the project folder has been created by the command, we technically have a fully-functional WordPress block registered as a plugin. So, let’s go ahead and drop the project folder into the wp-content/plugins
directory where you have WordPress installed (probably best to be working in a local environment), then log into the WordPress admin and activate it from the Plugins screen.
Now that our block is initialized, installed, and activated, go ahead and open up the project folder from at /wp-content/plugins/football-rankings
. You’re going to want to cd
there from the command line as well to make sure we can continue development.
These are the only files we need to concentrate on at the moment:
The other files in the project are important, of course, but are inessential at this point.
Here’s the code where I am importing useEffect()
, then wrapping it around the fetch()
code that RapidAPI provided:
Notice that I have left the return
function pretty much intact, but have included a note that confirms the football standings are rendered on the front end. Again, we’re only going to focus on the front end in this article — we could render the data in the Block Editor as well, but we’ll leave that for another article to keep things focused.
We define all this in our index.js
file:
OK, so WordPress now knows that the RapidAPI data we’re fetching is an object. If we open a new post draft in the WordPress Block Editor and save the post, the data is now stored in the database. In fact, if we can see it in the wp_posts.post_content
field if we open the site’s database in phpMyAdmin, Sequel Pro, Adminer, or whatever tool you use.
There are multiple ways to output the data on the front end. The way I’m going to show you takes the attributes that are stored in the database and passes them as a parameter through the render_callback
function in our football-rankings.php
file.
I like keeping a separation of concerns, so how I do this is to add two new files to the block plugin’s build
folder: frontend.js
and frontend.css
(you can create a frontend.scss
file in the src
directory which compiled to CSS in the build
directory). This way, the back-end and front-end codes are separate and the football-rankings.php
file is a little easier to read.
Referring back to the introduction to WordPress block development, there are editor.css
and style.css
files for back-end and shared styles between the front and back end, respectively. By adding frontend.scss
(which compiles to frontend.css
, I can isolate styles that are only intended for the front end.
Before we worry about those new files, here’s how we call them in football-rankings.php
:
Now that we have two new files we’re calling, we’ve gotta make sure we are telling npm
to compile them. So, do that in package.json
, in the scripts
section:
The only reason I’m going with the package.json
method is because I am already making use of the render_callback()
method.
In the rendering part, I am concentrating only on a single block. Generally speaking, you would want to target multiple blocks on the front end. In that case, you need to make use of document.querySelectorAll()
with the block’s specific ID.
I’m basically going to wait for the window to load and grab data for a few key objects from JSON and apply them to some markup that renders them on the front end. I am also going to convert the attributes
data to a JSON object so that it is easier to read through the JavaScript and set the details from JSON to HTML for things like the football league logo, team logos, and stats.
The “Last 5 games” column shows the result of a team’s last five matches. I have to manually alter the data for it since the API data is in a string format. Converting it to an array can help make use of it in HTML as a separate element for each of a team’s last five matches.
As far as styling goes, you’re free to do whatever you want! If you want something to work with, I have a full set of styles you can use as a starting point.
I styled things in SCSS since the @wordpress/create-block
package supports it out of the box. Run npm run start
in the command line to watch the SCSS files and compile them to CSS on save. Alternately, you can use npm run build
on each save to compile the SCSS and build the rest of the plugin bundle.
Check that out — we just made a block plugin that fetches data and renders it on the front end of a WordPress site.
We found an API, fetch()
-ed data from it, saved it to the WordPress database, parsed it, and applied it to some HTML markup to display on the front end. Not bad for a single tutorial, right?
Again, we can do the same sort of thing so that the rankings render in the Block Editor in addition to the theme’s front end. But hopefully keeping this focused on the front end shows you how fetching data works in a WordPress block, and how the data can be structured and rendered for display.
If you need help creating a digital marketing strategy for your business, don’t hesitate to contact one of Digidude’s consultants.
Post a Comment
You must be logged in to post a comment.