Return to Snippet

Revision: 60019
at October 16, 2012 06:27 by bitsculptor


Initial Code
// The register_post_type() function is not to be used before the 'init'.
add_action( 'init', 'my_custom_init' );

/* Here's how to create your customized labels */
function my_custom_init() {
	$labels = array(
		'name' => _x( 'Portfolio Galleries', 'post type general name' ), // Tip: _x('') is used for localization
		'singular_name' => _x( 'Portfolio Gallery', 'post type singular name' ),
		'add_new' => _x( 'Add New', 'Portfolio Gallery' ),
		'add_new_item' => __( 'Add New Portfolio Gallery' ),
		'edit_item' => __( 'Edit Portfolio Gallery' ),
		'new_item' => __( 'New Portfolio Gallery' ),
		'view_item' => __( 'View Portfolio Gallery' ),
		'search_items' => __( 'Search Portfolio Galleries' ),
		'not_found' =>  __( 'No Portfolio Galleries found' ),
		'not_found_in_trash' => __( 'No Portfolio Galleries found in Trash' ),
		'parent_item_colon' => ''
	);

	// Create an array for the $args
	$args = array( 'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => true,
		'menu_position' => null,
		'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'page-attributes' )
	); 

	register_post_type( 'portfolio-gallery', $args ); /* Register it and move on */
}
//add a custom message to the post message function

add_filter('post_updated_messages', 'gallery_updated_messages');
function gallery_updated_messages( $messages ) {

  $messages['portfolio-gallery'] = array(
    0 => '', // Unused. Messages start at index 1.
    1 => sprintf( __('Gallery updated. <a href="%s">View Gallery</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Gallery updated.'),
    /* translators: %s: date and time of the revision */
    5 => isset($_GET['revision']) ? sprintf( __('Gallery restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Gallery published. <a href="%s">View Gallery</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Gallery saved.'),
    8 => sprintf( __('Gallery submitted. <a target="_blank" href="%s">Preview Gallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Gallery scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Gallery</a>'),
      // translators: Publish box date format, see http://php.net/date
      date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Gallery draft updated. <a target="_blank" href="%s">Preview Gallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );

  return $messages;
}
// hook into the init action and call create_gallery_taxonomies() when it fires
add_action( 'init', 'create_gallery_taxonomies', 0 );
function create_gallery_taxonomies() {

	// Add new taxonomy, hierarchical
	$labels = array(
		'name' => _x( 'Categories', 'taxonomy general name' ),
		'singular_name' => _x( 'Category', 'taxonomy singular name' ),
		'search_items' =>  __( 'Search Categories' ),
		'popular_items' => __( 'Popular Categories' ),
		'all_items' => __( 'All Categories' ),
		'parent_item' => null,
		'parent_item_colon' => null,
		'edit_item' => __( 'Edit Category' ),
		'update_item' => __( 'Update Category' ),
		'add_new_item' => __( 'Add New Category' ),
		'new_item_name' => __( 'New Category Name' ),
		'separate_items_with_commas' => __( 'Separate categories with commas' ),
		'add_or_remove_items' => __( 'Add or remove categories' ),
		'choose_from_most_used' => __( 'Choose from the most used categories' )
	);
	register_taxonomy( 'gallery-category', 'portfolio-gallery', array(
		'hierarchical' => true,
		'labels' => $labels, /* NOTICE: the $labels variable here */
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'gallery-category' ),
	));
}

Initial URL

                                

Initial Description
This gives the code for creating a custom post type with all the necessary labels and even custom message support, and it gives the code for a custom taxonomy with that post type.

Initial Title
Custom post type, taxonomy, and messages

Initial Tags
post, wordpress

Initial Language
PHP