One of my favorite features of a commercial CMS we use is the Generic Content Module. This module allows you to create content outside of any particular page. You may then reference this content by simply dropping a control onto the page editor and selecting the content id. This is a wonderful solution for sharing content across pages. It also allows for easy translation.
WordPress 2.9 unfortunately does not have similar functionality to the Generic Content Module, so I set out to see how I can develop one on my own. Version 2.9 does not make this easy, since custom post types are not easy to create. The database supports them, but you are on your own in terms of an easy to use UI screen.
WordPress 3, however, easily supports new post types. This is done via register_post_type().
Here is the code required to add the new post type for my generic content module:
register_post_type('Generic Content', array(
'label' => __('Generic Content'),
'singular_label' => __('Generic Content'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array('title', 'editor', 'author')
));
Once the new post type is registered, we need a way to reference the post id in our content. We do this by registering a content filter that uses a regular expression to do a search and replace of tags. Here is my content filter:
add_filter('the_content', 'bcGenericContentFilter', 10);
function bcGenericContentFilter($content)
{
$newcontent = preg_replace_callback("/\(!(.*?)!\)/", 'bcTagMatchCallback', $content);
return $newcontent;
}
Here is the regular expression callback function:
function bcTagMatchCallback($match)
{
$title = $match[1];
$post = get_page_by_title($title, OBJECT, 'Generic Content');
if ($post->post_status != 'publish')
return '';
$content = $post->post_content;
return $content;
}
Now, create some generic content, and give it a title of “My Generic Content”. Create a post or a page, and inside your post use (!My Generic Content!) anywhere in your content and it will be replaced with the contents of the “My Generic Content” generic content.
The entire Generic Content Module can then be registered as a simple plugin:
post_status != ‘publish’)
return ”;
$content = $post->post_content;
return $content;
}
function bcGenericContentFilter($content)
{
$newcontent = preg_replace_callback(“/\(!(.*?)!\)/”, ‘bcTagMatchCallback’, $content);
return $newcontent;
}
register_post_type(‘Generic Content’, array(
‘label’ => __(‘Generic Content’),
‘singular_label’ => __(‘Generic Content’),
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘rewrite’ => false,
‘query_var’ => false,
‘supports’ => array(‘title’, ‘editor’, ‘author’)
));
?>
