Advertisements

register_post_type

Definition:
function register_post_type($post_type, $args = array() {}

Register a post type. Do not use before init.
A 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.

Parameters

  • string $post_type: Name of the post type.
  • array|string $args: See above description.

Return values

returns:the registered post type object, or an error object

Source code

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,

		'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,

		'_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,

		'public' => false, 'rewrite' => true, 'has_archive' => false, '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, 'show_in_menu' => null, 'show_in_admin_bar' => null,

	);

	$args = wp_parse_args($args, $defaults);

	$args = (object) $args;



	$post_type = sanitize_key($post_type);

	$args->name = $post_type;



	if ( strlen( $post_type ) > 20 )

			return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );



	// 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;



	// If not set, default to the setting for show_ui.

	if ( null === $args->show_in_menu || ! $args->show_ui )

		$args->show_in_menu = $args->show_ui;



	// If not set, default to the whether the full UI is shown.

	if ( null === $args->show_in_admin_bar )

		$args->show_in_admin_bar = true === $args->show_in_menu;



	// 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;



	// Back compat with quirky handling in version 3.0. #14122

	if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )

		$args->map_meta_cap = true;



	if ( null === $args->map_meta_cap )

		$args->map_meta_cap = false;



	$args->cap = get_post_type_capabilities( $args );

	unset($args->capabilities);



	if ( is_array( $args->capability_type ) )

		$args->capability_type = $args->capability_type[0];



	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 && ( is_admin() || '' != get_option('permalink_structure') ) ) {

		if ( ! is_array( $args->rewrite ) )

			$args->rewrite = array();

		if ( empty( $args->rewrite['slug'] ) )

			$args->rewrite['slug'] = $post_type;

		if ( ! isset( $args->rewrite['with_front'] ) )

			$args->rewrite['with_front'] = true;

		if ( ! isset( $args->rewrite['pages'] ) )

			$args->rewrite['pages'] = true;

		if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )

			$args->rewrite['feeds'] = (bool) $args->has_archive;



		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=");

2675

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: