Shortcut Functions For Registering Taxonomies And Post Types In WordPress

After I had created several custom taxonomies and post types, adding more by directly calling the register_taxonomy1 and register_post_type2 functions seemed like a huge waste of space. The following functions create a shortcut for registering taxonomies and custom post types in WordPress; just choose whether you'd like the taxonomy to resemble Post Tags or Categories when calling the function ucc_register_taxonomy, and specify an array of taxonomies for ucc_register_post_type.

function ucc_register_taxonomy ( $single , $plural , $slug , $type = 'tag' ) { $hierarchical = ( $type == 'category' ) ? 'true' : 'false';

$labels = array( 'name' => _x( $plural , 'taxonomy general name' ), 'singular_name' => _x( $single , 'taxonomy singular name' ), 'search_items' => __( 'Search ' . $plural ), 'all_items' => __( 'All ' . $plural ), 'parent_item' => __( 'Parent ' . $single ), 'parent_item_colon' => __( 'Parent ' . $single . ':' ), 'edit_item' => __( 'Edit ' . $single ), 'update_item' => __( 'Update ' . $single ), 'add_new_item' => __( 'Add New ' . $single ), 'new_item_name' => __( 'New ' . $single . ' Name' ) );

if ( !$hierarchical ) $labels = array_merge( $labels , array( 'parent_item' => null , 'parent_item_colon' => null , 'separate_items_with_commas' => __( 'Separate ' . strtolower( $plural ) . ' with commas' ), 'add_or_remove_items' => __( 'Add or remove ' . strtolower( $plural ) ), 'choose_from_most_used' => __( 'Choose from the most used ' . strtolower( $plural ) ) ) );

register_taxonomy( $slug , '' , array( 'hierarchical' => $hierarchical , 'labels' => $labels , 'show_ui' => true , 'query_var' => $slug , 'rewrite' => array( 'slug' => $slug ) ) ); }

function ucc_register_post_type ( $singular , $plural , $slug , $hierarchical , $supports , $menu_icon , $taxonomies ) { if ( empty( $supports ) || !is_array( $supports ) ) $supports = array( 'title' , 'editor' , 'author' , 'thumbnail' , 'excerpt' , 'trackbacks' , 'custom-fields' , 'comments' , 'revisions' );

if ( $hierarchical ) $supports = array_merge( $supports , array( 'page-attributes' ) );

if ( !is_array( $taxonomies ) ) $taxonomies = array( 'post_tag' , 'category' );

$labels = array( 'name' => _x( $plural , 'post type general name' ) , 'singular_name' => _x( $singular , 'post type singular name' ) , 'add_new' => _x( 'Add New' , strtolower( $singular ) ) , 'add_new_item' => __( 'Add New ' . $singular ) , 'edit_item' => __( 'Edit ' . $singular ) , 'new_item' => __( 'New ' . $singular ) , 'view_item' => __( 'View ' . $singular ) , 'search_items' => __( 'Search ' . $plural ) , 'not_found' => __( 'No ' . strtolower( $plural ) . ' found' ) , 'not_found_in_trash' => __( 'No ' . strtolower( $plural ) . ' found in Trash' ) , 'parent_item_colon' => '' );

register_post_type( $slug , array( 'labels' => $labels , 'public' => true , 'publicly_queryable' => true , 'show_ui' => true , 'query_var' => $slug , 'rewrite' => array( 'slug' => $slug ) , 'capability_type' => 'post' , 'hierarchical' => $hierarchical , 'supports' => $supports , 'menu_position' => 5 , 'menu_icon' => $menu_icon , 'taxonomies' => $taxonomies ) ); }

add_action( 'init', 'ucc_register_taxonomies' ); function ucc_register_taxonomies() { ucc_register_taxonomy( 'Ingredient' , 'Ingredients' , 'ingredient' , 'tag' ); ucc_register_taxonomy( 'Chapter' , 'Chapters' , 'chapter' , 'category' );

ucc_register_post_type( 'Recipe' , 'Recipes' , 'recipe' , false , '' , '/icons/16/fugue/cutlery.png' , array( 'category' , 'ingredient' , 'chapter' ) ); }

References

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>