logo
  • Home
  • About
  • Plugins

Subscribe to Articles

Don’t mess with my Toot Toot

Author: andrew Category: Uncategorized Tags: Content Types, Organisation, Plugin, Plugin-Practices

Thursday
Oct 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:

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.


Share:
image image image image image

Comments

Christopher Ross (http://thisismyurl.com)

October 9th, 2008 at 7:28 pm

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.

Matt Kern (http://mattkern.com)

October 9th, 2008 at 7:29 pm

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

Ptah Dunbar (http://ptahdunbar.com)

October 9th, 2008 at 7:43 pm

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

Andrew Rickmann (http://www.wp-fun.co.uk)

October 9th, 2008 at 8:32 pm

Ptah, no chance.

Simon (http://webdemar.com)

October 10th, 2008 at 12:17 pm

Thanks for sharing Andrew. Very refreshing ideas!

Ian Stewart (http://themeshaper.com/)

October 10th, 2008 at 2:28 pm

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

Charles (http://www.nwesource.com)

November 24th, 2008 at 7:03 am

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

Vicky (http://garinungkadol.com)

December 8th, 2008 at 2:08 am

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.

Helen Atwood

February 16th, 2009 at 7:50 am

your blog is awsome


Configurable Plugins
Interview with Brian Gardner

Recommended

WP Remix Banner

Archives

  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007

Tags

2.7 2.8 Admin Advertising Air Blogging blogs Cache Cliche Coding Comments Content Types CSS Curry Death development Disqus Features Habari image Interview Licensing Modes Monday Poll New Features Organisation Personal Platform Plugin Plugin-Practices plugins Plugin update Poll premium Readers Reviews Search Simplification Snippets Spam Themery Themes tools User Interface Widgets WLTC

Copyright 2009 Fun with WordPress - All Rights reserved.

Wordpress theme by: WPUnlimited