
Last night a question was asked on twitter about the potential for adding alternative content types for WordPress. I replied.
I went so far as to suggest that it should be pretty easy to add a new content type and in return received a challenge, and this post is the result. I have written a plugin to add a very simple new type of post and I am going to try and explain the component parts of that plugin here. I don’t intend to include a lot of code; my intent is to explain the general process that I used, but the plugin will be attached to the end of the post for them that want to see how I did it.
There may be better and easier ways of doing it than this but this is my version.
I needed this really simple. The point of the plugin was not to make a finished article but just to demonstrate how to, so I decided to create a twitter clone of sorts. Two fields would be editable, title and content, the content would be limited to 140 characters, and there would be no drafts or editing. Adding and deleting only.
These new posts would be called Toots.
To add a new type of content there are really two options: create a new table, or use the existing posts table.
The posts table was fairly recently amended to allow alternative post types. This, in combination with the fact that there are already a lot of functions that can be used instead of having to write my own made it a no-brainer for this exercise. I haven’t used any database code at all.
Adding a different type of post is very very simple. Once all the form handling, and validation is removed this small function is what actually makes the magic happen:
[php]
function new_toot( $title , $content ) {
global $user_ID;
//get the relevent post vars and save away
// Create post object
$my_post = array();
$my_post['post_title'] = $title;
$my_post['post_content'] = $content;
$my_post['post_status'] = 'publish';
$my_post['post_date'] = date('Y-m-d H:i:s');
$my_post['post_author'] = $user_ID;
$my_post['post_type'] = 'toot';
$my_post['post_category'] = array(0);
//Insert the post into the database
return wp_insert_post( $my_post );
}
All that really happens here is that I create an array and pass it to wp_insert_post. A function that already exists in WordPress. Deleting a post is even easier.
Managing this content means adding a new write panel and a new manage page which respectively pass commands to the plugin and trigger the add and delete functions. So far so simple.
The next part of adding new content types is new theme pages, and defining a hierarchy of theme pages. So first it is necessary to create a custom URL.
I’ve written before about the process of creating a custom URL but there are effectively three processes involved:
So when a user enters blogname/toots/19/ for example you can access this with $wp_query->get(‘toots’) and use it to make decisions.
With that data available the template selection process can be interrupted and if a specific toot has been chosen, as per the URL above, a single toot page can be served and if toot is selected without an ID a toot archive can be served, or it can fall back to the home page, or archive page.
Obviously this is all academic if the content can’t be output, so we need a loop:
When WordPress first loads the plugin creates a new query, in the same way as posts are created, but instead stores them in the $toot_list global variable, instead of the $posts global variable.
When the template selection happens, if a specific toot has been selected then a new query is carried out that replaces the current list, so only one toot is available.
The remaining functionality is handled by a list of functions that access the $toot_list global variable. I won’t list them all but the key ones are included here:
This replicates that have_posts function that is part of the loop. It checks the global $toot_list to see if there are toots to display.
This is the second part of the loop. This gets the next toot and makes it available to the other functions using the $toot global variable. This is equivalent to the $post global variable.
So the toot loop looks like this:
[php]
if (have_toots()) : while (have_toots()) : the_toot();
You can see it is practically the same as the normal post loop, but with toots instead of posts.
The is the equivalent the_title, it echos the title.
The it echos the date.
The echos the content.
The echos the content.
So a full loop might look like:
[html]
There is obviously more that would be done to make a fully complete new content type but hopefully this demonstrates how you can go about it, should you want to take things further.
The plugin can be downloaded here.
your blog is awsome
[...] Rickmann demonstrates how to create custom content types in WordPress
I hadn't realized that it was this simple to have custom post types. This is something I'll be playing around with. Thanks for the excellent resource.
“so far so simple” – you got me… you programmers! :) That's why I'm forced to pay you over and over again… The concept in the article is great, I don't want my clients being able to break the code when I have set up their pages but they want to change the content.
[...] second post was kicked off by a tweet asking how to add new content types in WordPress, I thought it should be simple, and as I had a long walk that day to mull it over I planned the [...]
[...] has been a lot of buzz regarding using WordPress as a CMS lately, with many clever solutions that come up. Today, we will be a bit lazy try to use a plugin that is made precisely for [...]
Thanks for laying this out with such a great example, Andrew. I’m going to have to play around with this.
[...] Don
Thanks for sharing Andrew. Very refreshing ideas!
Ptah, no chance.
This looks very promising. Once this gets more developed and more streamline within WordPress, will you then stop mentioning the H word? lol
Good post and a good song reference to match. You don’t find that very often.
I will check out the plugin. Looks very interesting, thanks.
Matt
Thanks for this, while content types are not something everybody looks for it’s something that I have an interest in for auto opening Word etc.