Automatically Add Filler Text to Blank WordPress Pages and Posts

Automatically Add Filler Text to Blank WordPress Pages and Posts

When you’re setting up a new WordPress site it can be somewhat disheartening to load up a newly installed theme in your browser only to find it empty and limp, devoid of your wonderful words.

But it doesn’t have to be that way. Adding filler text is a handy way to fill out your site while you’re customizing your theme. If you’ve ever done this manually, you know that copying and pasting text into new pages and posts can be time consuming and a tad boring.

To make the process easier, this awesome code snippet adds a few paragraphs of lorem ipsum placeholder text to any blank page or post when you hist “Publish.”

Lorem Ipsum

The text is retrieved from www.lipsum.com and is cached site-wide for one hour using the set_transient and get_transient functions.

That hour gives you enough time to replace the filler text with your own content – and also ensures you aren’t stuck with lorem ipsum on your site if you forget to replace the dummy text.

Simply add this code to your theme’s functions.php file and you’re good to go:

{code type=php}add_filter (‘the_content’, ’emw_custom_filter_the_content’);

/**
* Returns Lorem Ipsum text for blank pages
*
* @param string $content – the page’s current contents
* @return string
*/
function emw_custom_filter_the_content ($content) {
if ($content == ”) {
if ($c = get_transient (‘lipsum’))
return $c;
$content = wp_remote_get (‘http://www.lipsum.com/feed/json’);
if (!is_wp_error($content)) {
$content = json_decode (str_replace (“\n”, ‘

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

‘, $content[‘body’]));
$content = ‘

‘.$content->feed->lipsum.’

‘;
set_transient (‘lipsum’, $content, 3600); // Cache the text for one hour
return $content;
}
} else
return $content;
}

If you want to keep the filler text for longer than an hour you can edit the set_transient expiration value to whatever you want. If you want the filler text on your site for a whole day, replace the 17th line of the code with this:

{code type=php}set_transient (‘lipsum’, $content, 60*60*12);

Or, if you want to keep the filler text indefinitely (though surely you’ve got your own sweet words to fill your site with and sing to your readers!), you can set the value to never expire:

{code type=php}set_transient (‘lipsum’, $content, 0);

Thanks to Mark Barnes for this handy snippet!

If you want to add more than just filler text, check out Joe Foley’s helpful post, Lorem Ipsum: Top 5 WordPress Dummy Content Filler Plugins.

Do you use filler content or even theme demo content when you’re setting up a new WordPress site? Tell us about it in the comments below.

Image credits: bburky.