Automatically add Lorem Ipsum text to blank WordPress pages

If you’re creating a new WordPress theme, it can be helpful to have some filler text in your pages. The snippet below adds a few paragraphs of lorem ipsum text to any blank post or page by filtering the_content. The text is retrieved from the www.lipsum.com site, and is cached, site-wide, for one hour using the set_transient and get_transient functions.

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", '</p><p>', $content['body']));
			$content = '<p>'.$content->feed->lipsum.'</p>';
			set_transient ('lipsum', $content, 3600); // Cache the text for one hour
			return $content;
		}
	} else
		return $content;
}

Speak Your Mind

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.