888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

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.

Post Image The Easy Peasy Way
Wp attachment functions?
26 comments
page 1065
Dont Mess With My Toot Toot
Wordpress custom content?
16 comments
page 599
Post Image The Easy Peasy Way
Inserting an image url in comments?
26 comments
page 1065
Using Your Own Url Shortener
String shortner?
4 comments
page 1190
Quick N Dirty Admin Login Screen
My admin login page?
no comment
page 128
Using Your Own Url Shortener
How to build a shortner link?
4 comments
page 1190
Post Image The Easy Peasy Way
Get post attacments?
26 comments
page 1065
Theming Habari Vs Wordpress
Habari vs wordpress?
13 comments
page 440
Are Child Themes The Best Option
Wp child themes?
15 comments
page 1262
Upload From Url
Upload to url?
6 comments
page 326
Silence Is Golden
Silence is golden wordpress?
3 comments
page 213
How To Add Sidebars To A Theme
Html hot add a side bar?
10 comments
page 1053
Fun With Sidebar Tabs Styling
Position of the tabs thematic?
2 comments
page 336
How To Add Sidebars To A Theme
How to add picture to the wordpress sidebar on sidebarphp?
10 comments
page 1053
Using Your Own Url Shortener
Short url?
4 comments
page 1190
Why I Ditched Disqus
Styling disqus widgets?
5 comments
page 1175
Wordpress 25 Exif Fields
Wordpress exif?
12 comments
page 230
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Converting Wordpress Themes To Habari
Habari timthumb?
one comment
page 694
Wordpress 25 Exif Fields
Habari timthumb?
12 comments
page 230
Fun With Sidebar Tabs Styling
Tabs with html css on same page javascript?
2 comments
page 336
Using Your Own Url Shortener
How to have own url short?
4 comments
page 1190
Post Image The Easy Peasy Way
Insert conditional image php wp?
26 comments
page 1065
How To Add Sidebars To A Theme
Wordpress sidebar above main sidebars?
10 comments
page 1053
Using Your Own Url Shortener
Run short url using htaccess?
4 comments
page 1190
Html 5 Gallery
Html5 simple wordpress theme?
6 comments
page 1305
Dont Mess With My Toot Toot
New posttype not displayed wordpress?
16 comments
page 599
Quick N Dirty Replacement Text
Wpupdatepost object?
no comment
page 122
Updating Code Snippets Here
Fun wordpress plugin?
one comment
page 1338
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
How To Add Sidebars To A Theme
Wp register sidebars?
10 comments
page 1053
  1 query every 2084 seconds, 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