New WordPress Plugin: SuperCPT

I recently launched a new plugin on the WordPress plugin repository: SuperCPT. SuperCPT is a WordPress Plugin to help developers build insanely easy and attractive custom post types, custom post meta boxes and fields, and custom taxonomies. Install it from the WordPress.org Plugin Repository.

Description

SuperCPT adds object wrappers for Custom Post Types, Custom Taxonomies, and Custom Post Meta “for coders, by coders.” Simply put, SuperCPT:

  • DRYs up the Custom Post Type and Custom Taxonomy process (e.g. automatically adds the name to all the labels),
  • allows you to set default options for all your Custom Post Types and Taxonomies,
  • significantly simplifies the process of creating, saving, and displaying Custom Post Meta,
  • is sexy! Your custom fields are styled to look great and SuperCPT comes with 350 awesome icons courtesy of glyphicons.com.

More Information

Instructions

Depending on when and where you’re declaring your Custom Post Types and Taxonomies, you have different options for which action to hook onto. after_setup_theme is the safest bet, but if you’re referencing this in another plugin, plugins_loaded is a good choice too. To avoid a fatal error if something goes awry, you should check to see if the class Super_Custom_Post_Type exists before referencing it. Don’t worry about keeping up, reference code is below.

Custom Post Types

To define a new Custom Post Type, instantiate the Super_Custom_Post_Type class with a string for the post type. For example,

$movies = new Super_Custom_Post_Type( 'movie' );

It works very much like register_post_type. The first thing you gained by using this is that the labels all got setup with either ‘Movie’ or ‘Movies’. If our post type were ‘indie-film’, the labels would have “Indie Film” and “Indie Films” as appropriate. Of course, you do have the ability to set the plural word in cases such as goose/geese. You also gained the ability to define your own custom post type default args through a filter. Lastly, you gained access to Super_Custom_Post_Type‘s parent class, Super_Custom_Post_Meta, for fast, clean, intuitive custom post meta boxes and fields, which we’ll go into shortly.

Lastly, if you’ve built a lot of custom post types, you’re probably sick and tired of the pushpin icon. SuperCPT comes with 350 gorgeous icons courtesy of glyphicons.com that are extremely easy to implement. Here’s what it looks like:

$movies->set_icon( 'film' );

Custom Taxonomies

To define a new Custom Taxonomy, much like with Custom Post Types, you instantiate Super_Custom_Taxonomy with a string for the term name. For example:

$actors = new Super_Custom_Taxonomy( 'actor' );

Again, we got free labels for doing this, using either ‘Actor’ or ‘Actors’ as appropriate, without needing to specify the 16 labels individually.

Custom Post Meta

Custom Post Meta is where SuperCPT shines the brightest, because this process is typically the most time-consuming.Super_Custom_Post_Meta is a free-standing class that can be added to any post type, even built-in post types (posts and pages). This class has a method add_meta_box which does the bulk of the work, and somewhat mimics the WordPress function. Here’s an example:

$movies->add_meta_box( array(
    'id' => 'features',
    'fields' => array(
        'tagline' => array( 'type' => 'text' )
    )
) );

The method add_meta_box takes an array of parameters (unlike the core function which takes normal ordered arguments). id is the only required attribute, and that becomes the ID of the meta box as well as the title (this will get converted into “words” for the title, e.g. "movie_details" would become “Movie Details”). fields is an array of all the fields in the meta box. It’s an associative array, where the keys in the array are the field names and the values are another associative array of attributes for the field. The keys closely reflect the HTML attributes in the resulting field, and any key not known by the plugin will in fact become an HTML attribute (e.g. passing 'data-src' => 'foo' would become the HTML attribute data-src="foo" in the field). See the reference for the full gamut of options, both for the add_meta_box argument array and the fields array.

Long story short, using this class means you don’t have to do any additional work to store data, retrieve data, style the boxes, and so on.

Helper Functions

SuperCPT has a number of helper functions for displaying and working with your post meta.

Demo Code

Here is a full body of demo code:

function scpt_demo() {
    if ( ! class_exists( 'Super_Custom_Post_Type' ) )
        return;

    $demo_posts = new Super_Custom_Post_Type( 'demo-post' );

    # Test Icon. Should be a square grid.
    $demo_posts->set_icon( 'show_thumbnails' );

    # Taxonomy test, should be like tags
    $tax_tags = new Super_Custom_Taxonomy( 'tax-tag' );

    # Taxonomy test, should be like categories
    $tax_cats = new Super_Custom_Taxonomy( 'tax-cat', 'Tax Cat', 'Tax Cats', 'category' );

    # Connect both of the above taxonomies with the post type
    connect_types_and_taxes( $demo_posts, array( $tax_tags, $tax_cats ) );

    # Add a meta box with every field type
    $demo_posts->add_meta_box( array(
        'id'      => 'demo-fields',
        'context' => 'normal',
        'fields'  => array(
            'textbox-demo'        => array(),
            'textarea-demo'       => array( 'type' => 'textarea' ),
            'wysiwyg-demo'        => array( 'type' => 'wysiwyg' ),
            'boolean-demo'        => array( 'type' => 'boolean' ),
            'checkboxes-demo'     => array( 'type' => 'checkbox', 'options' => array( 'one', 'two', 'three' ) ),
            'radio-buttons-demo'  => array( 'type' => 'radio',    'options' => array( 'one', 'two', 'three' ) ),
            'select-demo'         => array( 'type' => 'select',   'options' => array( 1 => 'one', 2 => 'two', 3 => 'three' ) ),
            'multi-select-demo'   => array( 'type' => 'select',   'options' => array( 'one', 'two', 'three' ), 'multiple' => 'multiple' ),
            'date-demo'           => array( 'type' => 'date' ),
            'label-override-demo' => array( 'label' => 'Label Demo' )
        )
    ) );

    # Add another CPT to test one-to-one (it could just as easily be one-to-many or many-to-many) relationships
    $linked_posts = new Super_Custom_Post_Type( 'linked-post', 'Other Post', 'Other Posts' );
    $linked_posts->add_meta_box( array(
        'id'      => 'one-to-one',
        'title'   => 'Testing One-to-One relationship',
        'context' => 'side',
        'fields'  => array(
            'demo-posts'   => array( 'type' => 'select', 'data' => 'demo-post' ),
            'side-wysiwyg' => array( 'type' => 'wysiwyg' )
        )
    ) );
    $linked_posts->set_icon( 'cogwheels' );
}
add_action( 'after_setup_theme', 'scpt_demo' );

10 thoughts on “New WordPress Plugin: SuperCPT

  1. 3 times now I’ve installed and uninstalled SuperCPT trying to get it to work instead of showing me the long list of icons I can use *when* I get around to installing that plugin. Here’s what the page title shows and the only tab is the “Icons” tab.

    SuperCPT Settings
    Icons

    When I investigate what files are in the ‘edit’ area, there doesn’t seem to be anything about icons yet (because there shouldn’t be yet).
    What do I have to do to get the plugin to find the files that show me all the great stuff on the screen, instead of just the icons for the other plugin?

    Thank you.
    slg

    • Hi Sylvia,
      Thanks for checking out the plugin!

      The only thing currently in the settings page are the icons. At one point I had envisioned there would be a whole slew of settings in there, separated by tab, but how it came to be really didn’t require any. Thanks for pointing out that it says that, it’s unnecessary at this point, so I’ll make it just say icons.

      Ultimately, there’s nothing you can do with SuperCPT in the WordPress Admin — it’s all done in code. This plugin is for developers who want to build custom post types faster and with less repetition than using the core functions, but still want to do so in code. If that describes you, check out the wiki and watch the video, and that should tell you everything you need to know. If that doesn’t describe you, then you might want to check out a plugin with a user interface, like Custom Post Type UI. I hope this answers your question!

      Cheers,
      Matt

  2. Awesome plugin, I absolutely love it and it’s saving me time on my projects, so thank you!

    I have a (possibly nooby) question for you. I was checking out your code, and in your admin_hooks function in the Super_CPT class, you have the following lines:

    if ( apply_filters( ‘scpt_show_admin_menu’, true ) )
    $scpt_admin = new SCPT_Admin;

    I browsed the rest of your plugin but could not find where you registered the filter ‘scpt_show_admin_menu’.
    – If you run apply_filter on an unnamed filter, since your second param is true, does apply_filter just return true?
    – Did you perhaps originally have plans to expand the admin_menu and that’s why this is here? Or are you allowing developers to hook in and modify the menu by adding this here?

    Thanks for your time!

  3. Thanks for the excellent plugin, it helped me a lot, create custom post type without your plugin is extremely boring!
    Create custom post type in wordpress should be easy and intuitive as this. Thank you so much!

  4. I’m currently trying to familiarize myself with your plugin. I need to add two more “supports” options to the defaults in Super_Custom_Post_Type, and I see that you have a filter that can do this. Would you mind showing me an example of how to do that? I would very much appreciate it.

  5. Pingback: whereiskenya.com

  6. Pingback: horoscope

Leave a Reply