Custom Post Types in WP3


Published on

I gave a brief overview of the new Custom Post Types in WordPress 3. I talked about how we can use them and did a demo of creating our 1st Custom Pots Type plugin. The plugin can be downloaded from

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Custom Post Types in WP3

  1. 1. Custom Post Types & TaxonomiesGregg Henrytriple G interactive @GreggHenry
  2. 2. Hello. · BS in Computer Science from Bowling Green State University. · 10 Years of Web Development Experience. · 5 Years of Coding WordPress Themes and Plugins. · 3 Years of running triple G interactive.
  3. 3. So...What is a Custom Post Type? · The same way you can create regular posts, you can create custom ones with more creative control. · Not just a custom post, but custom content. · Been around since WP2.9.Examples · Portfolio, Video Library, Testimonials, FAQs, Recipes, etc.Advantanges · Easier on developers; no more relying on custom fields. · Easier for your clients to use; have their own navigation link.Take Note · Don’t get carried away with custom post types!
  4. 4. Wait...What is a Taxonomy? · Put in very easy to understand words - its just a way to classify and group things. · Built in taxonomies are Categories and Tags · Been around since WP 2.3, but now extremely handy with Custom Post TypesCustom Taxonomies Example · genres for books
  5. 5. Our 1st Plugin with Custom Post Types· Create a Recipe Class to house our plugin· Register the post type ‘recipe’· Add Custom Meta Box (no more ugly custom fields)· Create custom categories using taxonomies· Tips on how to display the new content we have· Profit?
  6. 6. Step 1: Let’s Get Started· Create your folder recipress in wp-content/plugins/· Create recipress.php in wp-content/plugins/recipress/<?php/*Plugin Name: ReciPressPlugin URI: Add a basic Recipe Plugin for your website using custom post types.Author: Gregg HenryVersion: 1.0Author URI:*/
  7. 7. Step 2: Create The Class to house our Plugin· define any constant variables· give the class a unique namedefine(RECIPRESS_POST_TYPE, ‘recipe’);class Recipress{}
  8. 8. Step 3: Tap into Plugin Activation Hook· register_activation_hook function lets us run specific code when the plugin is activat-ed.· We need to flush the rewrite rulesdefine(RECIPRESS_POST_TYPE, ‘recipe’);class Recipress{ function Install() { flush_rewrite_rules(); }} //end Recipress Classregister_activation_hook(__FILE__, array(‘Recipress’,’Install’));
  9. 9. Step 4: Initialize and Register the post type ‘recipe’· Add an Initialize function to your class.· Register the post type in this function. function Initialize() { ... register_post_type(RECIPRESS_POST_TYPE,$args); ... }· Outside of our class we will call Initialize to hook into ‘init’add_action(‘init’, array(‘Recipress’, ‘Initialize’));
  10. 10. Register the Taxonomy (Category)· We can use the built-in taxonomy for categories, but we wont.· We’ll create a custom taxonomy so that it’s specific to Recipes.· Add the below code to the end of the Initialize function. register_taxonomy( ‘cat’, array(RECIPRESS_POST_TYPE), array(‘hierarchical’ => true, ‘show_ui’ => true, ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘category’ ), ));
  11. 11. Step 5: Showing Categories in column layout· Adding a new column to your custom posts list in the Admin. function edit_column_title($defaults) { $defaults[‘cat’] = ‘Category’; return $defaults; } function edit_column_value($column_name, $post_id) { $taxonomy = $column_name; ... echo join( ‘, ‘, $post_terms ); }· Outside of our class we will call a filter and action to add info.add_filter( ‘manage_recipe_posts_columns’, array(‘Recipress’,’edit_column_title’) );add_action(‘manage_recipe_posts_custom_column’, array(‘Recipress’,’edit_column_value’), 10, 2);
  12. 12. Step 6: Add a Custom Meta Box· A custom post type needs a custom meta box :-)· Utilize any type of form element· We’ll setup a textbox forCalorie Count in the sidebar. function meta_options(){ global $post; $custom = get_post_custom($post->ID); $calories = $custom[“calories”][0]; ?> <label>Calories:</label><input name=”calories” value=”<?php echo$calories; ?>” /> <?php }
  13. 13. Front End Display· Now that we have the admin created, how do we display what we created?Displaying a Single Custom Post· Create single-recipe.php to make a single post page.Looping Through your Custom Posts· Place this snippet of code right before the loop.<?php query_posts( ‘post_type=recipe’); ?>Display custom meta box content· Do it the same way in the meta_options function.$custom = get_post_custom($post->ID);$calories = $custom[“calories”][0];echo $calories;
  14. 14. Recap· Custom Post Types are new for WP3.· Creative control over custom content.· Easier on developers; no more relying on custom fields.· Easier for your clients to use; have their own navigation link.· Great for things that can be cataloged, but not for categorizing· We walked through creating a plugin that allows a user to enter Recipes · We registered our post type of ‘recipe’ · We registered our taxonomy ‘cat’ · We added a custom meta box · Then we displayed the content for the user.
  15. 15. Resources···
  16. 16. Thank You. · Questions?