Quantcast
Channel: Designs By Nick the Geek
Viewing all articles
Browse latest Browse all 30

Custom Post Types Made Easy

$
0
0

Post types aren’t really that hard to start with. You start with some template code, then make a few changes and bam! Now you have a custom post type. However, teh code can get clunky if you register several CPTs. On this blog I have a single CPT, for the child themes used by my Child Theme Matrix. I use some template code I picked up when I first read about custom post types. You can find that article and a lot of information on CPTs on Justin’s article on Custom Post Types.

However, sometimes your client wants a CPT for videos, image galleries, and …. When faced with a growing list of post types and no reasonable objection (sometimes post types really are the right answer but most of the time I do try to convince my clients to work within posts and pages, after all, we did that exclusively until a little while ago), registering each CPT can become an annoying task.

Recently I had a job where we started with just 1 CPT, then 2, 3, 4, …. It wasn’t all at once but I eventually looked at the code and realized I can get rid of a lot of repeated lines. It makes the code prettier if not more efficient. I had looked at other code for registering CPTs and realized I can make it even more simple.

This is what I’m using now

/**
 * Registers a post type with default values which can be overridden as needed.
 * 
 * @author Nick the Geek
 * @link https://designsbynickthegeek.com/tutorials/custom-post-types-made-easy
 * 
 * @uses sanitize_title() WordPress function that formats text for use as a slug
 * @uses wp_parse_args() WordPress function that merges two arrays and parses the values to override defaults
 * @uses register_post_type() WordPress function for registering a new post type
 * 
 * @param string $title title of the post type. This will be automatically converted for plural and slug use
 * @param array $args overrides the defaults
 */
function ntg_register_post_type( $title, $args = array() ){
    
    $sanitizedTitle = sanitize_title( $title );
    
    $defaults = array(
            'labels' => array(
                'name' => $title . 's',
                'singular_name' => $title,
                'add_new_item' => 'Add New ' . $title,
                'edit_item' => 'Edit ' . $title,
                'new_item' => 'New ' . $title,
                'search_items' => 'Search ' . $title . 's',
                'not_found' => 'No ' . $title . 's found',
                'not_found_in_trash' => 'No ' . $title . 's found in trash'
            ),
            '_builtin' => false,
            'public' => true,
            'hierarchical' => false,
            'taxonomies' => array( ),
            'query_var' => true,
            'menu_position' => 6,
            'supports' => array( 'title', 'editor', 'thumbnail', 'author', 'comments', 'genesis-seo' ),
            'rewrite' => array( 'slug' => $sanitizedTitle ),
            'has_archive' => true
        );
    
    $args = wp_parse_args( $args, $defaults );
    
    $postType = isset( $args['postType'] ) ? $args['postType'] : $sanitizedTitle;
    
    register_post_type( $postType, $args );
    
}

Basically, there is only one required parameter, the Title. However, you can change any other value by using the array. So you can do this

// Register post type "foo"
ntg_register_post_type( 'Foo' );

// Register post type "bar" without an archive
ntg_register_post_type( 'Bar', array( 'has_archive' => false ) );

// Register post type "geeks" with the title Nick's Peeps
ntg_register_post_type( 'Nick\'s Peeps', array( 'postType' => 'geeks', rewrite => array( 'slug' => 'geek' ) ) );

I’m sure you can see how this can save a lot of time, you might even build an array like this.

$postTypes = array(
    'Foo'               => '',
    'Bar'               => array( 'has_archive' => false ),
    'Nick\'s Peeps'     => array( 'postType' => 'geeks', rewrite => array( 'slug' => 'geek' ) )
);

foreach( $postTypes as $title => $args )
    ntg_register_post_type( $title, $args );

How are you using post types? Do you use functions or classes to handle your post type registration?


Viewing all articles
Browse latest Browse all 30

Trending Articles