I’ve been impressed by how WordPress integrates custom content types into it’s interface whilst maintaining the ease of use of the CMS. Rather than using a database approach, it is all code based. There are a lot of tutorials available but I found the one by Think Vitamin the easiest to run through. My one issue with it, however, is that there is a lot of code required for each type you create. I was recently re-developing a site to using a CMS from using CodeIgniter and decided to give the custom types a go myself.
Looking around the current plugins which perform similar tasks, they all offer GUIs. I feel that code code offer a better alternative because:
- You can easily integrate dropdowns or select lists using content elsewhere in the site. E.g. you may want to have a select box for users so they can select which clubs they are a member of
- You don’t have to worry about import/export functionality, you just move your types with your theme
Since there wasn’t anything around that I felt would do the job effectively I set-up a plugin myself (WordPress download details to follow). Below is a screenshot of one of the types created through the plugin:
There’s a couple of points about it:
- Each meta-block in WordPress can contain several fields, making seperation of content clear and easy to follow
- Each type can specify it’s own taxonomies (more than one is possible, if needed)
- You can leave in or leave out the default title, editor and featured image fields
- You can use more than one editor if necessary using the plugin
- You can have as many image fields as you want and they will show up separately within the interface
- The “Clubs” field pulls list of clubs from the Clubs custom type
- No more messy all in one lists within your posts page, each type has it’s own content list
Here’s the code needed to create the people type:
//person type: $people = new CustomType('person', 'Person', 'People', array('supports'=>array('title','thumbnail'))); $people->addTaxonomy( "person", array( "hierarchical" => true, "label" => "Departments", "singular_label" => "Department", "rewrite" => true ) ); //bio section: $bio = $people->addSection("bio", "Biography", Section::CONTEXT_NORMAL, Section::PRIORITY_CORE); $bio->addField("current", "Current employee", Field::FIELD_TYPE_CHECKBOX, true); $bio->addField("jobtitle", "Role", Field::FIELD_TYPE_TEXT); $bio->addField("shortdesc", "Short description", Field::FIELD_TYPE_TEXT); $bio->addField("bio", "Biography", Field::FIELD_TYPE_MCE); $bio->addField("desertisland", "3 desert island items", Field::FIELD_TYPE_TEXT); $bio->addField("specialtalent", "Special talent", Field::FIELD_TYPE_TEXT); //Links section: $links = $people->addSection("links", "Links", Section::CONTEXT_SIDE, Section::PRIORITY_DEFAULT); $links->addField("twitter", "Twitter", Field::FIELD_TYPE_TEXT); $links->addField("facebook", "Facebook", Field::FIELD_TYPE_TEXT); $links->addField("linkedin", "Linkedin", Field::FIELD_TYPE_TEXT); $links->addField("blog", "Blog", Field::FIELD_TYPE_TEXT); $links->addField("website", "Website", Field::FIELD_TYPE_TEXT); //People - club memberships: $clubs = $people->addSection("clubs", "Clubs", Section::CONTEXT_SIDE, Section::PRIORITY_DEFAULT); //if clubs exist load them into array for use in selection: $clubQuery = new WP_Query(array('post_type' => 'club', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC')); $clubArray = array(); while ($clubQuery->have_posts()) { $clubQuery->the_post(); $clubArray[get_the_ID()] = get_the_title(); } $clubs->addField("clubs", "Clubs", Field::FIELD_TYPE_SELECT, array(), $clubArray);
The code is really basic OO code and as a result, it’s very quick to write. It’s easy to create new types from the template above. There is no reason to have to use custom fields if you don’t need them. Using the following will just create a new “Clubs” type with no other fields than the standard. Creating this as a custom type though isolates the content in the CMS.
//club type: $club = new CustomType( 'club', 'Club', 'Our Clubs', array('supports'=>array('title', 'editor', 'thumbnail')) );
A proper usage guide will follow once the plugin has been uploaded to WordPress
