Use a plugin to handle custom post types?

I’ve seen some tutorials showing how to create custom post types – the process seems straightforward enough. The issue I have with this is that the custom type information is part of the theme, and not handled by the WP core. So if the theme is switched for another, they custom type information is lost, and must be ported to the new theme.

I’ve seen some plugins that handle the custom types for you. It seems to me that this is kept separate from the theme, and would thus be more portable, provided the plugin is kept activated. There would of course be a need for the theme to template and style the custom type, but it wouldn’t have to maintain the type itself.

Read More

Is it worthwhile to use a plugin for this purpose?

Related posts

Leave a Reply

2 comments

  1. Hi @Grant Palin:

    The register_post_type() function is really agnostic to theme or plugin; you can use it in an 'init' hook in either place, it really depends on what you are trying to accomplish.

    For example, if I’m setting up a custom site for a specific client I’ll probably just register the post types in the theme. On the other hand, if I’m trying to create a reusable custom post type (“Event” might be an example) then I might register the Event post type in an Event-specific plugin.

    However be aware that unless your post type is similar to a standard Post you’ll likely still need specific theme support for it anyway. And with the pending release of Post Formats in v3.1, there really aren’t many good reasons to define custom post types that are similar in use-case to a standard Post so you really have six of one, half dozen of the other.

    Also, I frequently put custom post type registrations for a new site in an include file called by the theme’s functions.php file unless and until I get to the point of a fully generic feature in which case I’ll move to a plugin (most of my clients have me writing reusable functionality that they then sell to their clients, and most of my work is related to custom post types. I rarely do work on code that will only run on a single website FWIW, JMMV.)

    One thing I will say, I would avoid using plugins that provide a UI for creating custom post types and custom taxonomies like Custom Post Type UI and similar except for:

    1. When you want to do some proof-of-concept prototyping or

    2. When you want to empower an end-user that you don’t want to give FTP access.

    Why? Because these plugins all store their custom post type and taxonomy registrations in the database which makes it very difficult to version control them and also makes it much harder to launch a new version of the site from existing source code. Those UI plugins are really only best used when learning, tinkering, exploring, and/or doing a quick and dirty “what-if” / “proof-of-concept”. (Of course the UI plugins are wonderful those those uses.)

    What’s more, the code to register a custom post type and a custom taxonomy is so easy to learn that there’s really no reason for a WordPress professional to develop and deliver a system that doesn’t hardcode its custom post types and custom taxonomies into PHP code, either in the theme or in a plugin, your choice. Matter of fact, I’m about to extract the CPT-UI plugin from a client project and replace with hardcoded PHP registrations this coming week.

    Hope this helps. (If not, ask for more.)

  2. My suggestion for you would be to utilize the EXCELLENT class created by Dimas which is called WP Alchemy. You can find it along with examples here:
    http://farinspace.com/wpalchemy-metabox/

    **Summary of WPAlchemy Metabox Class for WordPress”:
    The WPAlchemy_MetaBox PHP class can be used to create WordPress meta boxes quickly. It will give you the flexibility you need as a developer, allowing you to quickly build custom meta boxes for your themes and plugins.

    Key Features and Benefits

    1. Easy to learn and integrate: good
      documentation and support is always
      important (I use the code myself and
      keep it up-to-date). Integration is
      a snap, as simple as including the
      class and using it.
    2. Easy setup code: some of the details
      involved in saving, retrieving and
      working with the meta data are
      abstracted to ease development.
    3. Flexible usage: the class acts as an
      aid for meta box development. By
      design you can use the class
      functions or your current
      development practices, which ever
      you feel most comfortable with for
      your development.
    4. HTML and CSS separation: the HTML
      and CSS for your meta boxes remain
      separate from the core code, you can
      design your meta boxes to your
      liking, providing you the greatest
      flexibility during development.

    Defining a Meta Box is Easy

    // include the class in your theme or plugin
    include_once 'WPAlchemy/MetaBox.php';
    
    // include css to help style our custom meta boxes
    if (is_admin()) wp_enqueue_style('custom_meta_css',TEMPLATEPATH . '/custom/meta.css');
    
    $custom_metabox = new WPAlchemy_MetaBox(array
    (
        'id' => '_custom_meta',
        'title' => 'My Custom Meta',
        'template' => TEMPLATEPATH . '/custom/meta.php'
    ));
    enter code here
    

    That’s it! The above code shows the basic definition needed to setup a custom meta box.

    How to Use It
    All of the functions in this class are very WordPress friendly. If you are familiar with the WordPress Loop, you should have no problem using this class.

    When working on your meta box template file (meta.php) there are a few variables available to you:

    $post; // this is the current post, use $post->ID for the current post ID
    $metabox; // this is the meta box helper object
    $mb; // same as $metabox, a shortcut instead of writing out $metabox
    $meta; // this is the meta data
    

    More info can be found here: http://farinspace.com/wpalchemy-metabox/