register_post_type: Source code

By the Web Warlock, Wednesday, 23/Jun/2010 20:09

/**
 * Register a post type. Do not use before init.
 *
 * A simple function for creating or modifying a post type based on the
 * parameters given. The function will accept an array (second optional
 * parameter), along with a string for the post type name.
 *
 *
 * Optional $args contents:
 *
 * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
 * - description - A short descriptive summary of what the post type is. Defaults to blank.
 * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
 * - exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
 * - publicly_queryable - Whether post_type queries can be performed from the front page.  Defaults to whatever public is set as.
 * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
 * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
 * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
 * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
 * - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities.
 * - hierarchical - Whether the post type is hierarchical. Defaults to false.
 * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
 * - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form.  Do remove_meta_box() and add_meta_box() calls in the callback.
 * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.  Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
 * - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
 * - permalink_epmask - The default rewrite endpoint bitmasks.
 * - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $post_type as slug.
 * - query_var - false to prevent queries, or string to value of the query var to use for this post type
 * - can_export - true allows this post type to be exported.
 * - show_in_nav_menus - true makes this post type available for selection in navigation menus.
 * - _builtin - true if this post type is a native or "built-in" post_type.  THIS IS FOR INTERNAL USE ONLY!
 * - _edit_link - URL segement to use for edit link of this post type.  Set to 'post.php?post=%d'.  THIS IS FOR INTERNAL USE ONLY!
 *
 * @since 2.9.0
 * @uses $wp_post_types Inserts new post type object into the list
 *
 * @param string $post_type Name of the post type.
 * @param array|string $args See above description.
 * @return object the registered post type object
 */
function register_post_type($post_type, $args = array()) {
	global $wp_post_types, $wp_rewrite, $wp;
 
	if ( !is_array($wp_post_types) )
		$wp_post_types = array();
 
	// Args prefixed with an underscore are reserved for internal use.
	$defaults = array(
		'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
		'_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
		'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
		'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
		'permalink_epmask' => EP_PERMALINK, 'can_export' => true, 'show_in_nav_menus' => null
	);
	$args = wp_parse_args($args, $defaults);
	$args = (object) $args;
 
	$post_type = sanitize_user($post_type, true);
	$args->name = $post_type;
 
	// If not set, default to the setting for public.
	if ( null === $args->publicly_queryable )
		$args->publicly_queryable = $args->public;
 
	// If not set, default to the setting for public.
	if ( null === $args->show_ui )
		$args->show_ui = $args->public;
 
	// Whether to show this type in nav-menus.php.  Defaults to the setting for public.
	if ( null === $args->show_in_nav_menus )
		$args->show_in_nav_menus = $args->public;
 
	// If not set, default to true if not public, false if public.
	if ( null === $args->exclude_from_search )
		$args->exclude_from_search = !$args->public;
 
	if ( empty($args->capability_type) )
		$args->capability_type = 'post';
 
	$args->cap = get_post_type_capabilities( $args );
	unset($args->capabilities);
 
	if ( ! empty($args->supports) ) {
		add_post_type_support($post_type, $args->supports);
		unset($args->supports);
	} else {
		// Add default features
		add_post_type_support($post_type, array('title', 'editor'));
	}
 
	if ( false !== $args->query_var && !empty($wp) ) {
		if ( true === $args->query_var )
			$args->query_var = $post_type;
		$args->query_var = sanitize_title_with_dashes($args->query_var);
		$wp->add_query_var($args->query_var);
	}
 
	if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
		if ( !is_array($args->rewrite) )
			$args->rewrite = array();
		if ( !isset($args->rewrite['slug']) )
			$args->rewrite['slug'] = $post_type;
		if ( !isset($args->rewrite['with_front']) )
			$args->rewrite['with_front'] = true;
		if ( $args->hierarchical )
			$wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
		else
			$wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
		$wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
	}
 
	if ( $args->register_meta_box_cb )
		add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
 
	$args->labels = get_post_type_labels( $args );
	$args->label = $args->labels->name;
 
	$wp_post_types[$post_type] = $args;
 
	add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
 
	foreach ( $args->taxonomies as $taxonomy ) {
		register_taxonomy_for_object_type( $taxonomy, $post_type );
	}
 
	return $args;
}
 

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment