Save Time And Effort With Reusable WordPress Content

As WordPress site owners, we often find that we need to repeat the same content across posts and pages: advertisements and promotions, legal notices, special announcements, reproduction notices.

It may be content that needs to appear on every page or just particular posts but the point is we want to just want to create that item once and then have an easy way to include it wherever is appropriate.

In this WordPress Weekend Project, we’ll take at look at saving you valuable time and effort by creating and using reusable content.

Recycle symbol on the WWP logo
Save time and create common content once and reuse it throughout a site

Converting any content that you use in more than one location in your site into reusable content is going to save you a great deal of time and potential issues.

If you are duplicating an important message across your site then every time that message needs an update, you are going to have to make multiple edits which is time-consuming, both in locating and testing the new message, and prone to mistakes.

Reusable content allows you to centralize your common or global content and then simply embed it. Any changes made to content are instantly reflected across your site, making it a safe and efficient method.

There are a number of potential solutions for reusable content and I’ve picked the Reusable Text Blocks plugin from Hal Gatewood for a number of reasons:

  1. Stores content in a custom content type – I like this because it means that my reusable content has its own menu option in the admin interface and won’t appear in searches
  2. Provides embedding via function or shortcode – so we can either include the content directly in specific pages or posts by using the shortcode, or have more widespread embedding by adding the function call to a template, the theme’s functions.php or in a plugin
  3. Provides a widget – This is pretty handy and is a good replacement for the humble text widget

When you install the plugin, you’ll notice a new menu option, Text Blocks. The Text Blocks work in almost identical fashion to pages, except that the listing is customized to give valuable information such as the shortcode.

Screenshot of the text block edit screen
Familiar environment makes creating text blocks easy; combining with other plugins makes adds flexibility

Content is added in the familiar WordPress WYSIWYG editor meaning that you get all the formatting options, you can include media and other shortcodes.

That ability to include shortcodes makes reusable content very powerful. You might remember that in my article on tailored messages, I used the WordPress Conditional Content plugin. Using that plugin in conjunction with the Reusable Text Blocks plugin enables us to create some pretty sophisticated global content that can show and hide text based on the visitors referrer, their role on our site or any querystring variable (useful for detecting Google Campaigns).

Adding Reusable Text Blocks To Your Content

Once we have created a Text Block, we have three choices for displaying it on our site.

Adding As Shortcode To Pages And Posts

If we want to be very specific about where our reusable content is displayed we can simply add it to the relevant posts and pages by using a shortcode:

Screenshot of post edit screen with shortcode in the body
Just insert the shortcode anywhere in your content

The shortcode text is available on the Text Block listing page.

When the post or page is displayed, the shortcode will be replaced with the Text Block content.

Adding A Function To A Theme Template

If we want to show the content across the site, then it makes more sense to use the show_text_block function provided by the plugin.

Let’s say, for example, we want the “My Example Text Block” Text Block to appear at the bottom of each post of our site. We’d find the relevant template (for TwentyFourteen this is content.php) and add this immediately after the call to the_content();

{code}
<?php if(function_exists(‘show_text_block’)) { echo show_text_block(‘my-example-text-block’); } ?>
{/code}

Or, if we want to use the Text Block’s id instead of the slug

{code}
<?php if(function_exists(‘show_text_block’)) { echo show_text_block(1); } ?>
{/code}

We could also consider using the filter, the_content :

{code}

function add_text_block( $content ){

if ( is_single() ) {
remove_filter( ‘the_content’ , ‘add_text_block’  , 999);
$content .= show_text_block( ‘my-first-reusable-text-block’ );
}

return $content;

}

add_filter( ‘the_content’ , ‘add_text_block’ , 999);

{/code}

This will achieve the same outcome as editing the template but means that we don’t have to change any templates. If we created our own small plugin with this code, rather than say adding to our theme’s functions.php, then it will work with any theme.

You’ll notice that we’ve given the filter a low priority. That’s because we want the filter to be processed last as we need to remove it before the call to show_text_block to prevent getting stuck in an endless loop.

This is a really simple example. You could also get very sophisticated with the call to the show_text_block function by checking, for example, for a certain category, whether the home page is being shown or if the request is for a particular custom post type (such as a product).

Show As A Widget

The Reusable Text Blocks plugin provides its own widget which basically acts as  a replacement for the humble but immensely useful standard text widget.

Rather than entering the content in the limited widget textarea, the Reusable Text widget allows us to select our content from a drop-down list of Text Blocks that we have created.

That makes for very easy text widget creation. However, during my testing I couldn’t get shortcodes in my Text Block to work when using the new widget. So, what if we want to use our conditional shortcodes to tailor the content, what then?

Well, it turns out that embedding the Text Block shortcode (as we did in a post or page) in a normal text widget works fine.

Of course, you do need to switch on shortcode processing in text widgets first. But as fellow WPMU DEV writer Rae showed us in her recent article on handy WordPress hacks, this is as easy as adding this code to either functions.php or a plugin:

{code}

add_filter( ‘widget_text’ , ‘do_shortcode’ );

{/code}

Used wisely and imaginatively, putting common content into reusable text blocks can not only save time and effort but, when combined with other plugins, can also provide sophisticated content delivery with little to no coding.

Could your site benefit from reusable text blocks?