April 12, 2010

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:

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:

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:

<?php if (have_toots()) : while (have_toots()) : the_toot(); ?>
<h3><?php the_toot_date(); ?>: <?php the_toot_title(); ?></h3>
<p><?php the_toot_content(); ?></p>
<?php endwhile; ?>
<?php endif; ?>

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 title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1734s, 1s ago, in 0.02s.
 __
(__)
   `
 colin

You should look into wp3.0, set to release within the month, which makes registering custom content types a core functionality. You should be able to do all this with an array and function in functions.php, with just another array for each custom type, not a whole custom plugin for each.

 __
(__)
   `
 Helen Atwood

your blog is awsome

 __
(__)
   `
 How To Create Custom WordPress Content Types | Castup

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

 __
(__)
   `
 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.

 __
(__)
   `
 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.

 __
(__)
   `
 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 [...]

 __
(__)
   `
 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 [...]

 __
(__)
   `
 Ian Stewart

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

 __
(__)
   `
 links for 2008-10-10

[...] Don

 __
(__)
   `
 Simon

Thanks for sharing Andrew. Very refreshing ideas!

 __
(__)
   `
 Andrew Rickmann

Ptah, no chance.

 __
(__)
   `
 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

 __
(__)
   `
 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

 __
(__)
   `
 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.


0.01s