The Forge

Using custom post types to extend WordPress – Part 1

Wednesday, October 12th, 2011 at 8:25 pm

One of the most powerful capabilities of WordPress is the ability for developers to create custom post types. Custom post types have transformed WordPress from being essentially a blogging platform into a full blown CMS able to deliver sophisticated functionality within a well designed and thought-out framework.

The idea behind custom post types is that you can create any form of content object within the existing WordPress post framework doing away with the need to create custom database tables and giving you the immediate benefit of all the functionality that already exists for posts. In addition to custom post types developers can additionally create their own custom taxonomies allowing them to easily add ways to classify their content objects and provide sophisticated searches and cataloging.

To demonstrate how easy and powerful the custom post capability is, here is a walk-through of a recent plugin I developed for an automobile dealer to allow them to list their inventory of cars on their website.

The Requirement

Our customer required a simple plugin to allow them to present their stock of cars to their users from within their WordPress website. Each car was to consist of a title, description, images, price, details of its make and model and summary of its key features. Website users need to be able to search for cars by their make, model and price. The backend user interface needed to be easy to use and efficient to allow relatively non-technical sales staff to add and maintain their forecourt inventory.

To achieve this we opted to implement the plugin as follows:

  • A custom type ‘vehicle’ that is used to store vehicles the dealer has for sale
  • A custom taxonomy ‘makemodel’ used to identify the make and model of the vehicle (for example Ford->Mustang). This custom taxonomy will be used to allow users to search by make or model.
  • A custom taxonomy ‘features’ to tag the vehicle with features (for example air conditioning or electric windows).

Adding the custom post type

The custom post type feature of WordPress really is incredibly powerful. Simply by registering a new post type, an incredible amount of functionality is immediately achieved.

Custom post types are registered using the register_post_type function. This function should be called from within a an ‘init‘ action.

register_post_type( 'vehicle', array(
  'labels' => array(
    'name' => __( 'Vehicles' ),
    'singular_name' => __( 'Vehicle' ),
    'add_new_item' => __("Add New Vehicle"),
    'edit_item' => __("Edit Vehicle"),
    'new_item' => __("New Vehicle"),
    'view_item' => __("View Vehicle"),
    'search_items' => __("Search Vehicles"),
    'not_found' => __( 'No Items found.' )
  ),
  'public' => true,
  'has_archive' => true,
  'menu_position' => 41,
  'menu_icon' => plugin_dir_url( __FILE__ ).
                 "/images/car.png",
  'supports' => array(
    'title',
    'editor',
    'thumbnail',
    'excerpt',
    'revisions'
   ),
));

 

Adding the Taxonomies

The taxonomies are added using the register_taxonomy function. As with register_post_type,  this is added from within an ‘init’ action.

register_taxonomy('makemodel','vehicle',array(
  'hierarchical' => true,
  'show_ui' =>true,
  'query_var' => true,
  'public' => true,
  'show_in_nav_menus' => true,
  'rewrite' => array(
    'slug' => "makes",
    'hierarchical'=> true,
    'with_front' => true
  ),
  'labels' => array(
    'name' => _x('Makes and Models','taxonomy general name'),
    'singular_name' => _x('Make/Model','taxonomy singular name'),
    'search_items' =>  __( 'Search Makes and Models' ),
    'all_items' => __( 'All Makes and Models' ),
    'parent_item' => __( 'Parent Make' ),
    'parent_item_colon' => __( 'Parent Make:' ),
    'edit_item' => __( 'Edit Make/Model' ),
    'update_item' => __( 'Update Make/Model' ),
    'add_new_item' => __( 'Add New Make/Model' ),
    'new_item_name' => __( 'New Make/Model Name' ),
    'menu_name' => __( 'Makes and Models' ),
   )
 ));

What we have achieved

Through this two simple steps we have achieved a huge amount of functionality and capability. The register_post_type adds the new ‘vehicle’ type to the admin navigation, provides a basic search page and allow us to create new ‘vehicle’ instances.

In my next post I will describe how we can further develop the new plugin to add extra user interface to allow the sale price of the vehicle to be entered and customise the ‘vehicle’ search to allow the price to be shown in the search results.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Find out what Coreware can do for your business contact us now