Ferodynamics Network

popular: profile privacy, mobile privacy

October 9, 2008

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.

What I decided to create

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.

Defining the new post type

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.

Theme pages

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:

  1. Tell WordPress to flush out the existing URL rules
  2. This triggers a process to reacquire them, at which point you can insert the new rules
  3. Add to the list of query string values that WordPress looks for, so you can use this to make decisions later

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.

Displaying the content: creating a loop

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:

have_toots

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.

the_toot

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_toot_title

The is the equivalent the_title, it echos the title.

the_toot_date

The it echos the date.

the_toot_content

The echos the content.

the_toot_rewind

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.

Download

The plugin can be downloaded here.

Wordpress Chat
one comment
page 1308
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Silence Is Golden
3 comments
page 213
Questions About Habari For Wordpress Users
6 comments
page 424
Theming Habari Vs Wordpress
13 comments
page 440
My Experience Of Flexx
4 comments
page 1026
Plugin Update Fun With Photo Data 2
one comment
page 815
Post Image The Easy Peasy Way
26 comments
page 1065
Categories Vs Tags Either Neither Or Both
12 comments
page 7
Gaining Benefits From Plugins
8 comments
page 1167
Fun With Theme Widgets
24 comments
page 867
Beware Wp Cache
8 comments
page 1310
Six Million Ways To Die Choose One
14 comments
page 1128
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Beware Wp Cache
8 comments
page 1310
Wp Polls Reviewed
one comment
page 58
Fun With Photo Data
12 comments
page 330
Html 5 Gallery
6 comments
page 1305
Fun With Sidebar Tabs Styling
2 comments
page 336
Using Your Own Url Shortener
4 comments
page 1190
Html 5 Gallery
6 comments
page 1305
My Experience Of Flexx
4 comments
page 1026
Fun With Sidebar Tabs
193 comments
page 57
Html 5 Gallery
6 comments
page 1305
Fun With Plugins
27 comments
page 14
Wordpress 25 Exif Fields
12 comments
page 230
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Beware Wp Cache
8 comments
page 1310
Quick N Dirty Replacement Text
no comment
page 122
Theming Habari Vs Wordpress
13 comments
page 440
Fun With Sidebar Tabs Styling
2 comments
page 336
Beware Wp Cache
8 comments
page 1310
  updated 1 seconds ago
Monday, 7am
Helen Atwood

your blog is awsome

Tuesday, 11pm
How To Create Custom WordPress Content Types | Castup

[...] Rickmann demonstrates how to create custom content types in WordPress

Monday, 2am
Vicky

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.

Monday, 7am
Charles

“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.

Monday, 12am
1 Year Old – A review of the past year of Fun with WordPress – WP FUN

[...] 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 [...]

Monday, 11am
Creating Custom Content Type with Flutter Plugin — WPCandy — WordPress Themes, Plugins, Tips, and Tricks

[...] 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 [...]

Friday, 2pm
Ian Stewart

Thanks for laying this out with such a great example, Andrew. I’m going to have to play around with this.

Friday, 1pm
links for 2008-10-10

[...] Don

Friday, 12pm
Simon

Thanks for sharing Andrew. Very refreshing ideas!

Thursday, 8pm
Andrew Rickmann

Ptah, no chance.

Thursday, 7pm
Ptah Dunbar

This looks very promising. Once this gets more developed and more streamline within WordPress, will you then stop mentioning the H word? lol

Thursday, 7pm
Matt Kern

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

Thursday, 7pm
Christopher Ross

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.