WordPress 3.0 is coming soon and one of the biggest new features is the ability to add custom post types. Making these custom post types is pretty easy, below is a simple but complete example of creating a link type custom post.
Register the Custom Post
The first thing you see here is the register_post_type function which starts everything. You can read about this on the codex page or Justin Tadlock’s post. These two pages should get you up to speed fast.
Actions and Filters
The next bit of code are all the callbacks. The first two deal with adding the meta box and saving it. The next two change the columns in the WordPress admin when viewing a list of the custom posts. The last is for using custom template names.
The Meta Box
The first two functions link_url_meta_box() and save_post_data() deal with the added meta box. I’ve kept things simple here so it should be easy to follow what’s happening.
Custom Columns
When looking through the list of your custom post you’ll probably want to display custom columns so it’s easier to find what you want. The nav_columns and custom_nav_columns control this: nav_columns sets everything up, and custom_nav_columns determines what will be listed there.
Custom Template Names
WordPress by default will use “single-link.php” for our template name since we named the custom post type “link“. But if you wanted to use a different template name for instance “foorbarlink.php” you can change it using this part of the code. If you’re happy with WordPress’ default naming you can exclude this completely.
Below is the function for a link type post, one that you might use in a tumble log. It’s a basic example of how to use the Custom Post Type feature in WordPress 3.0+
19 Comments
Leave a CommentThanks for this really useful, I’ve tried to use it in the latest wordpress 3.0, copy and pasted in functions.php and get a blank page.
I should be doing something wrong for sure.
Best
14th May 2010
Where are you getting the blank page? It sounds like a formatting error in functions.php
14th May 2010
Once you click “Publish” or Update, the wp-admin/posts.php comes up as a blank page
29th May 2010
You have a PHP error somewhere. Make sure there are no blank lines at the end of your functions.php file, or better yet remove any ?> at the end.
Also make sure you’re running the latest version of WordPress.
29th May 2010
Cheers Curtis, I found this really useful.
Chris
1st Jun 2010
Hi Curtis,
Thanks for this..
I copied ur code entirely on functions.php
I am just wondering why I get 404 errors when i publish the page..
I am a newbie to this stuffs, I am wondering if I did something wrong?
Thanks
Cheers,
Mich
1st Jul 2010
Not sure why you would get 404 errors, should just forward back to the same post or give a blank page.
What’s the URL in the address bar after you save?
1st Jul 2010
The post title is Michelle.. just to test it.
At its permalink, it is composed of the root/link/michelle
the URL is something like this
http://mysite.com/link/michelle
Would there be away to change the permalink and remove the /link/ so it will become
http://mysite.com/michelle
I don’t understand where does it get the parent page link..
Thank you so much
Cheers,
Mich
1st Jul 2010
Remove this line:
'rewrite' => array('slug'=>'link')6th Jul 2010
Got caught up with somethings to do causing delaying in extending my thanks to you Curtis..
that rewrite thing works..
thanks and best regards,
Mich
19th Aug 2010
I have the same problem with the page turning blank. As soon as I put your code it the admin presents a completely blank page.
I’ve reinstalled wordpress from scratch and can’t find the problem. I’ve tried different code for custom post types as well as editing functions.php in different editors.
Can you suggest anything it might be?
19th Jul 2010
Try adding
define('WP_DEBUG', true);to your wp_config.php file, hopefully you’ll get some errors to track down the problem.A blank page is usually a php error, try removing the ?> at the end of functions.php.
21st Jul 2010
p.s. I tried removing the ‘rewrite’ line as suggested to know avail.
19th Jul 2010
Hey. I am trying to work out how to add a second wysiwyg to the UI for my custom post. Is that even possible though? lol
22nd Jul 2010
Thanks for the tip – I’m now getting the error:
“Fatal error: Call to undefined function add_action() in /Applications/MAMP/htdocs/wordpress/wp-includes/functions.php on line 3″
Can’t understand why though. hrmmm
24th Jul 2010
Solved my problem. I was editing the main wordpress functions.php rather than the one for the current theme.
24th Jul 2010
Thanks for the how-to. I was able to successfully create the custom post types I needed and that part works great — even the custom templates are *mostly* behaving like I expect. But I something odd is happening on the original post types. I see custom fields (blank) that match the ones I created for the custom post types. Is there a way to clean that up?
15th Sep 2010
Did you happen to name the custom post type ‘post’? That would add the meta boxes to the default post page.
Also check in admin_init() that the add_meta_box function isn’t adding it to ‘post’
Those are the only things I can think of right now that would cause your problem.
15th Sep 2010
Curtis, thanks for the response. I checked both of those cases:
The new post type is not ‘post’ and the admin_init() isn’t adding the add_meta_box to ‘post’.
Any other thoughts?
27th Sep 2010