Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a custom post type. It has meta boxes, and it also adds meta boxes to other post types. It comes with some convenience functions to find posts of its type or the meta data it adds to existing post types. It also has some helper functions to render markup into a template.

All this adds up to quite a few 'loose ends' in my code. I don't have too much experience developing for WordPress (my background is in .NET and C#) so I'm wondering where I can look for patterns to make my solution... more cohesive. For example guidelines for naming files and functions and organizing the code in my theme, or a framework for creating custom post types.

Although it's not hard to find plugins to create custom post types in WordPress, I prefer a 'code-first' approach, where I can commit everything to source control. Some plugins may have a suitable API, but this is usually not communicated on the WordPress website.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

I have been working on a theme template to speed up my work progress lately and custom post types where taking a lot of my time.

These are a few simple functions that may be able to help you

function theme_template_create_post_type( $post_type, $labels = array(), $post_args = array() ) {
    if ( !$labels || count( $labels ) < 10 ) {
        $labels = array(
            'name' => __( ucfirst( $post_type . 's' ) ),
            'singular_name' => __( ucfirst( $post_type ) ),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add New' ),
            'edit_item' => __( 'Edit ' . ucfirst( $post_type ) ),
            'new_item' => __( 'Add New' ),
            'view_item' => __( 'View ' . ucfirst( $post_type ) ),
            'search_items' => __( 'Search ' . ucfirst( $post_type . 's' ) ),
            'not_found' => __( 'No ' . $post_type . 's found' ),
            'not_found_in_trash' => __( 'No ' . $post_type . 's found in trash' ),
            'all_items' => __( 'All ' . ucfirst( $post_type . 's' ) )
        );
    }
    $default_post_args = array( 'public' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => 20, 'supports' => array( 'title', 'editor', 'thumbnail' ) );
    $post_args = wp_parse_args( $post_args, $default_post_args );
    $custom_post_args = array(
        'labels' => $labels,
        'public' => $post_args[ 'public' ],
        'show_ui' => $post_args[ 'show_ui' ],
        'capability_type' => $post_args[ 'capability_type' ],
        'hierarchical' => $post_args[ 'hierarchical' ], 
        'menu_position' => $post_args[ 'menu_position' ], 
        'supports' => $post_args[ 'supports' ],
        'rewrite' => array( 'slug' => $post_type )
    );
    register_post_type( $post_type, $custom_post_args );
}

function theme_template_create_updated_messages( $post_type ) {
    add_filter( 'post_updated_messages', function( $messages ) use( $post_type ) {
        global $post, $post_ID;
        $messages[ $post_type ] = array(
            0  => '',
            1  => sprintf( '%s updated. <a href="%s">View %s</a>', ucfirst( $post_type ), esc_url( get_permalink($post_ID) ), ucfirst( $post_type ) ),
            2  => 'Custom field updated.',
            3  => 'Custom field deleted.',
            4  => sprintf( '%s updated.', ucfirst( $post_type ) ),
            5  => isset($_GET['revision']) ? sprintf( '%s restored to revision from %s', ucfirst( $post_type ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
            6  => sprintf( '%s published. <a href="%s">View %s</a>', ucfirst( $post_type ), esc_url( get_permalink($post_ID) ), ucfirst( $post_type ) ),
            7  => sprintf( '%s saved.', ucfirst( $post_type ) ),
            8  => sprintf( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', ucfirst( $post_type ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ), ucfirst( $post_type ) ),
            9  => sprintf( '%3$s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %3$s</a>',  date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ), ucfirst( $post_type ) ),
            10 => sprintf( '%s draft updated. <a target="_blank" href="%s">Preview %s</a>', ucfirst( $post_type ), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ), ucfirst( $post_type ) ),
        );
        return $messages;
    });
}

function theme_template_create_post_title( $post_type, $post_title ) {
    $custom_filter = function( $title ) use( $post_type, $post_title ) {
        $screen = get_current_screen();
        if ( $post_type == $screen->post_type ) {
            $title = $post_title;
        }
        return $title;
    };
    add_filter( 'enter_title_here', $custom_filter );
}

You can call them like this:

/*
 * Custom post type [ slide ]
 */
add_action( 'init', function() {
    theme_template_create_post_type( 'slide', array(), array( 'public' => false, 'supports' => array( 'title', 'editor' ) ) );
    theme_template_create_updated_messages( 'slide' );
    theme_template_create_post_title( 'slide', 'Enter slide name here' );
});

Note you will require PHP Version 5.3.0 to use the anonymous function approach.

As for meta boxes I tend not to go into the code-first approach as I have been there and done that and encountered a lot of errors/troubles on my way. (Again I am not the best WordPress developer in the world so forgive me)

I use something called Advanced Custom Fields found here: http://wordpress.org/extend/plugins/advanced-custom-fields/ the free version (no plugins) does everything I ask and it already includes many features I need! (Along with looking really nice too)

Hope this helps :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.