Reusable blocks are a helpful feature within the WordPress block editor ecosystem. They allow you to save a group of blocks for future use on other pages. Once a reusable block has been created, the content within can be updated and pushed to all instances of that block across your website (pretty cool stuff).

One example of this pattern would be a global call to action (CTA) block. That block will be used on many pages across your website and changing the heading text or link on the reusable block would be much easier than changing it individually on each page.

Adding a reusable block to a page

Theme Template File Usage

Recently, I discovered something that makes these reusable blocks even more powerful – using them in template files. I had a situation where I needed a separate CTA to appear at the bottom of posts published in a specific category (let’s call it “breaking-news”). Having a solution that was programmatic was preferable to adding the reusable block to each post based on the category.

Find Block IDs

First, we need to identify the IDs for the reusable blocks we want to add to the template file. Navigate to the reusable block list via the “Manage Reusable blocks” link in the Page Options menu on an individual page. Then, select the reusable blocks you’d like to use from the list and make a note of their IDs from the URL bar.

Navigate to the reusable block edit page
Take note of the ID from the URL

Add Blocks To Template File

Now, with the reusable block IDs in hand, we can add something like the code below to our template file. In my case, I’m adding this to single.php, but it could be any template file. The apply_filters part takes the content for the reusable block and runs that through the_content filter to render the HTML we want to display.

<?php
    if ( has_category('breaking-news')  ) {
        $reuseable_block = get_post(1872); // Breaking News CTA block 
    } else {
        $reuseable_block = get_post(1813); // Default CTA block 
    }
    
    $reuseable_block_content = apply_filters( 'the_content', $reuseable_block->post_content);
    echo $reuseable_block_content;
?>

Now, when you view the template, the rendered HTML for our reusable block will be displayed. And, most importantly, any updates to the block will be reflected in the template.

It’s worth mentioning that I didn’t find any documentation on this method, so take it with a measure of skepticism. What worked for me may not yield the same results in your use case.

Did You Find This Helpful?

Join my biweekly digest of curated HTML, CSS and JavaScript news.

Matt Litzinger headshot

Matt Litzinger

Matt is a New Hampshire-based web developer focused on UX and digital accessibility.