/** * 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. * * @since 2.9.0 * @since 3.0.0 The `show_ui` argument is now enforced on the new post screen. * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing screen and post editing screen. * * @global array $wp_post_types List of post types. * @global WP_Rewrite $wp_rewrite Used for default feeds. * @global WP $wp Used to add query vars. * * @param string $post_type Post type key, must not exceed 20 characters. * @param array|string $args { * Array or string of arguments for registering a post type. * * @type string $label Name of the post type shown in the menu. Usually plural. * Default is value of $labels['name']. * @type array $labels An array of labels for this post type. If not set, post * labels are inherited for non-hierarchical types and page * labels for hierarchical ones. {@see get_post_type_labels()}. * @type string $description A short descriptive summary of what the post type is. * Default empty. * @type bool $public Whether a post type is intended for use publicly either via * the admin interface or by front-end users. While the default * settings of $exclude_from_search, $publicly_queryable, $show_ui, * and $show_in_nav_menus are inherited from public, each does not * rely on this relationship and controls a very specific intention. * Default false. * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search * results. Default is the opposite value of $public. * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type * as part of {@see parse_request()}. Endpoints would include: * * ?post_type={post_type_key} * * ?{post_type_key}={single_post_slug} * * ?{post_type_query_var}={single_post_slug} * If not set, the default is inherited from $public. * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the * admin. Default is value of $public. * @type bool $show_in_menu Where to show the post type in the admin menu. To work, $show_ui * must be true. If true, the post type is shown in its own top level * menu. If false, no menu is shown. If a string of an existing top * level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post * type will be placed as a sub-menu of that. * Default is value of $show_ui. * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. * Default is value $public. * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value * of $show_in_menu. * @type int $menu_position The position in the menu order the post type should appear. To work, * $show_in_menu must be true. Default null (at the bottom). * @type string $menu_icon The url to the icon to be used for this menu. Pass a base64-encoded * SVG using a data URI, which will be colored to match the color scheme * -- this should begin with 'data:image/svg+xml;base64,'. Pass the name * of a Dashicons helper class to use a font icon, e.g. * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty * so an icon can be added via CSS. Defaults to use the posts icon. * @type string $capability_type The string to use to build the read, edit, and delete capabilities. * May be passed as an array to allow for alternative plurals when using * this argument as a base to construct the capabilities, e.g. * array('story', 'stories'). Default 'post'. * @type array $capabilities Array of capabilities for this post type. $capability_type is used * as a base to construct capabilities by default. * {@see get_post_type_capabilities()}. * @type bool $map_meta_cap Whether to use the internal default meta capability handling. * Default false. * @type array $supports An alias for calling {@see add_post_type_support()} directly. * Defaults to array containing 'title' & 'editor'. * @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the * edit form. Do remove_meta_box() and add_meta_box() calls in the * callback. Default null. * @type array $taxonomies An array of taxonomy identifiers that will be registered for the * post type. Taxonomies can be registered later with * {@see register_taxonomy()} or {@see register_taxonomy_for_object_type()}. * Default empty array. * @type bool|string $has_archive Whether there should be post type archives, or if a string, the * archive slug to use. Will generate the proper rewrite rules if * $rewrite is enabled. Default false. * @type bool|array $rewrite { * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be * passed with any of these keys: * * @type string $slug Customize the permastruct slug. Defaults to $post_type key. * @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front. * Default true. * @type bool $feeds Whether the feed permastruct should be built for this post type. * Default is value of $has_archive. * @type bool $pages Whether the permastruct should provide for pagination. Default true. * @type const $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set, * inherits from $permalink_epmask. If not specified and permalink_epmask * is not set, defaults to EP_PERMALINK. * } * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type * key. If false, a post type cannot be loaded at * ?{query_var}={post_slug}. If specified as a string, the query * ?{query_var_string}={post_slug} will be valid. * @type bool $can_export Whether to allow this post type to be exported. Default true. * @type bool $delete_with_user Whether to delete posts of this type when deleting a user. If true, * posts of this type belonging to the user will be moved to trash * when then user is deleted. If false, posts of this type belonging * to the user will *not* be trashed or deleted. If not set (the default), * posts are trashed if post_type_supports('author'). Otherwise posts * are not trashed or deleted. Default null. * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or * "built-in" post_type. Default false. * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of * this post type. Default 'post.php?post=%d'. * } * @return object|WP_Error The registered post type object, or an error 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(); } // Sanitize post type name $post_type = sanitize_key($post_type); $args = wp_parse_args($args); /** * Filter the arguments for registering a post type. * * @since 4.4.0 * * @param array $args Array of arguments for registering a post type. * @param string $post_type Post type key. */ $args = apply_filters('register_post_type_args', $args, $post_type); $has_edit_link = !empty($args['_edit_link']); // Args prefixed with an underscore are reserved for internal use. $defaults = array('labels' => array(), 'description' => '', 'public' => false, 'hierarchical' => false, 'exclude_from_search' => null, 'publicly_queryable' => null, 'show_ui' => null, 'show_in_menu' => null, 'show_in_nav_menus' => null, 'show_in_admin_bar' => null, 'menu_position' => null, 'menu_icon' => null, 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null, 'supports' => array(), 'register_meta_box_cb' => null, 'taxonomies' => array(), 'has_archive' => false, 'rewrite' => true, 'query_var' => true, 'can_export' => true, 'delete_with_user' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d'); $args = array_merge($defaults, $args); $args = (object) $args; $args->name = $post_type; if (empty($post_type) || strlen($post_type) > 20) { _doing_it_wrong(__FUNCTION__, __('Post type names must be between 1 and 20 characters in length.'), '4.2'); return new WP_Error('post_type_length_invalid', __('Post type names must be between 1 and 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 = (bool) $args->show_in_menu; } // If not set, default 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 not set, default to false. if (null === $args->map_meta_cap) { $args->map_meta_cap = false; } // If there's no specified edit link and no UI, remove the edit link. if (!$args->show_ui && !$has_edit_link) { $args->_edit_link = ''; } $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); } elseif (false !== $args->supports) { // Add default features add_post_type_support($post_type, array('title', 'editor')); } if (false !== $args->query_var) { if (true === $args->query_var) { $args->query_var = $post_type; } else { $args->query_var = sanitize_title_with_dashes($args->query_var); } if ($wp && is_post_type_viewable($args)) { $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 (!isset($args->rewrite['ep_mask'])) { if (isset($args->permalink_epmask)) { $args->rewrite['ep_mask'] = $args->permalink_epmask; } else { $args->rewrite['ep_mask'] = EP_PERMALINK; } } if ($args->hierarchical) { add_rewrite_tag("%{$post_type}%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type={$post_type}&pagename="); } else { add_rewrite_tag("%{$post_type}%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type={$post_type}&name="); } if ($args->has_archive) { $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; if ($args->rewrite['with_front']) { $archive_slug = substr($wp_rewrite->front, 1) . $archive_slug; } else { $archive_slug = $wp_rewrite->root . $archive_slug; } add_rewrite_rule("{$archive_slug}/?\$", "index.php?post_type={$post_type}", 'top'); if ($args->rewrite['feeds'] && $wp_rewrite->feeds) { $feeds = '(' . trim(implode('|', $wp_rewrite->feeds)) . ')'; add_rewrite_rule("{$archive_slug}/feed/{$feeds}/?\$", "index.php?post_type={$post_type}" . '&feed=$matches[1]', 'top'); add_rewrite_rule("{$archive_slug}/{$feeds}/?\$", "index.php?post_type={$post_type}" . '&feed=$matches[1]', 'top'); } if ($args->rewrite['pages']) { add_rewrite_rule("{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?\$", "index.php?post_type={$post_type}" . '&paged=$matches[1]', 'top'); } } $permastruct_args = $args->rewrite; $permastruct_args['feed'] = $permastruct_args['feeds']; add_permastruct($post_type, "{$args->rewrite['slug']}/%{$post_type}%", $permastruct_args); } // Register the post type meta box if a custom callback was specified. 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); } /** * Fires after a post type is registered. * * @since 3.3.0 * * @param string $post_type Post type. * @param object $args Arguments used to register the post type. */ do_action('registered_post_type', $post_type, $args); return $args; }
/** * 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. * * 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. * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu, * false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'. * show_ui must be true. * - 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 string to use to build the read, edit, and delete capabilities. Defaults to 'post'. * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the * capabilities, e.g. array('story', 'stories'). * - capabilities - Array of capabilities for this post type. By default the capability_type is used * as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}. * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false. * - hierarchical - Whether the post type is hierarchical. Defaults to false. * - supports - An alias for calling add_post_type_support() directly. See {@link 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. By default post labels are used for non-hierarchical * types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}. * - permalink_epmask - The default rewrite endpoint bitmasks. * - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled. * - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct; * default will use $post_type as slug. Other options include 'with_front', 'feeds', and 'pages'. * - 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. 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|WP_Error the registered post type object, or an error 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, '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); $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; } // 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 && '' != 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="); } if ($args->has_archive) { $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive; $wp_rewrite->add_rule("{$archive_slug}/?\$", "index.php?post_type={$post_type}", 'top'); if ($args->rewrite['feeds'] && $wp_rewrite->feeds) { $feeds = '(' . trim(implode('|', $wp_rewrite->feeds)) . ')'; $wp_rewrite->add_rule("{$archive_slug}/feed/{$feeds}/?\$", "index.php?post_type={$post_type}" . '&feed=$matches[1]', 'top'); $wp_rewrite->add_rule("{$archive_slug}/{$feeds}/?\$", "index.php?post_type={$post_type}" . '&feed=$matches[1]', 'top'); } if ($args->rewrite['pages']) { $wp_rewrite->add_rule("{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?\$", "index.php?post_type={$post_type}" . '&paged=$matches[1]', 'top'); } } $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; }
/** * 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; }
/** * @ticket 33023 */ public function test_get_post_type_object_casting() { register_post_type('foo'); $before = get_post_type_object('foo')->labels; get_post_type_labels(get_post_type_object('foo')); $after = get_post_type_object('foo')->labels; $this->assertEquals($before, $after); _unregister_post_type('foo'); }
</p> </td> </tr> <tr> <th scope="row"> <label><?php _e('Configure Terms'); ?> </label> </th> <td> <?php foreach ($att_post_types as $pt) { ?> <?php $labels = get_post_type_labels(get_post_type_object($pt)); ?> <a target="_blank" href="<?php echo esc_html(admin_url('edit-tags.php?taxonomy=' . $this->get_taxonomy_name($att_name) . '&post_type=' . $pt)); ?> " class="button configure-terms"><?php echo $labels->name . ' '; _e('Terms'); ?> </a> <?php } ?> <p class="description"><?php _e('Perform CRUD operations on Terms for this attribute.'); ?>
private static function meta($object_id, $meta_key, $meta_value) { $prefix = WPSEO_Meta::$meta_prefix; WPSEO_Metabox::translate_meta_boxes(); if (0 !== strpos($meta_key, $prefix)) { return; } $key = str_replace($prefix, '', $meta_key); foreach (WPSEO_Meta::$meta_fields as $tab => $fields) { if (isset($fields[$key])) { $field = $fields[$key]; break; } } if (!isset($field, $field['title'], $tab) || '' === $field['title']) { return; } $post = get_post($object_id); $post_type_label = get_post_type_labels(get_post_type_object($post->post_type))->singular_name; self::log(sprintf(__('Updated "%1$s" of "%2$s" %3$s', 'stream'), $field['title'], $post->post_title, $post_type_label), array('meta_key' => $meta_key, 'meta_value' => $meta_value, 'post_type' => $post->post_type), $object_id, 'wpseo_meta', 'updated'); }
/** * Sets post type properties. * * @since 4.6.0 * @access public * * @param array|string $args Array or string of arguments for registering a post type. */ public function set_props($args) { $args = wp_parse_args($args); /** * Filters the arguments for registering a post type. * * @since 4.4.0 * * @param array $args Array of arguments for registering a post type. * @param string $post_type Post type key. */ $args = apply_filters('register_post_type_args', $args, $this->name); $has_edit_link = !empty($args['_edit_link']); // Args prefixed with an underscore are reserved for internal use. $defaults = array('labels' => array(), 'description' => '', 'public' => false, 'hierarchical' => false, 'exclude_from_search' => null, 'publicly_queryable' => null, 'show_ui' => null, 'show_in_menu' => null, 'show_in_nav_menus' => null, 'show_in_admin_bar' => null, 'menu_position' => null, 'menu_icon' => null, 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null, 'supports' => array(), 'register_meta_box_cb' => null, 'taxonomies' => array(), 'has_archive' => false, 'rewrite' => true, 'query_var' => true, 'can_export' => true, 'delete_with_user' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d'); $args = array_merge($defaults, $args); $args['name'] = $this->name; // 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'] = (bool) $args['show_in_menu']; } // If not set, default 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 not set, default to false. if (null === $args['map_meta_cap']) { $args['map_meta_cap'] = false; } // If there's no specified edit link and no UI, remove the edit link. if (!$args['show_ui'] && !$has_edit_link) { $args['_edit_link'] = ''; } $this->cap = get_post_type_capabilities((object) $args); unset($args['capabilities']); if (is_array($args['capability_type'])) { $args['capability_type'] = $args['capability_type'][0]; } if (false !== $args['query_var']) { if (true === $args['query_var']) { $args['query_var'] = $this->name; } else { $args['query_var'] = sanitize_title_with_dashes($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'] = $this->name; } 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 (!isset($args['rewrite']['ep_mask'])) { if (isset($args['permalink_epmask'])) { $args['rewrite']['ep_mask'] = $args['permalink_epmask']; } else { $args['rewrite']['ep_mask'] = EP_PERMALINK; } } } foreach ($args as $property_name => $property_value) { $this->{$property_name} = $property_value; } $this->labels = get_post_type_labels($this); $this->label = $this->labels->name; }
/** * Registers the post type. * * If the post type already exists, some of the arguments will be merged into the existing post type object. * * @since 0.5.0 */ public function register() { if (!$this->is_already_added()) { $_post_type_args = $this->args; unset($_post_type_args['title']); unset($_post_type_args['singular_title']); unset($_post_type_args['title_gender']); unset($_post_type_args['messages']); unset($_post_type_args['enter_title_here']); unset($_post_type_args['show_add_new_in_menu']); unset($_post_type_args['table_columns']); unset($_post_type_args['row_actions']); unset($_post_type_args['bulk_actions']); unset($_post_type_args['help']); unset($_post_type_args['list_help']); $_post_type_args['label'] = $this->args['title']; $_post_type_args['register_meta_box_cb'] = array($this, 'add_meta_boxes'); $post_type_args = array(); foreach ($_post_type_args as $key => $value) { if (null !== $value) { $post_type_args[$key] = $value; } } register_post_type($this->slug, $post_type_args); } else { // merge several properties into existing post type global $wp_post_types; if (is_array($this->args['supports'])) { foreach ($this->args['supports'] as $feature) { add_post_type_support($this->slug, $feature); } } if ($this->args['labels']) { // merge the slug as $name into the arguments (required for `get_post_type_labels()`) $wp_post_types[$this->slug]->labels = get_post_type_labels((object) array_merge($this->args, array('name' => $this->slug))); $wp_post_types[$this->slug]->label = $wp_post_types[$this->slug]->labels->name; } add_action('add_meta_boxes_' . $this->slug, array($this, 'add_meta_boxes'), 10, 1); } }
/** * {@inheritdoc} */ public static final function getLabels() { return get_post_type_labels(static::getDefinition()); }
/** * construit l'options (html) du post spécifié et ses fils * @param object $post * @param int $id_selected * @param int $current_post_id : post in loop * @param number $level * @return string */ function wall_get_post_types_option($post, $id_selected = 0, $current_post_id = 0, $level = 0) { $res = ''; if ($id_selected == $post->ID) { $selected = ' selected="selected"'; } else { $selected = ''; } $level_string = ''; for ($i = 0; $i < $level; $i++) { $level_string .= '-'; } $info = ''; if ($current_post_id == $post->ID) { $post_type_label = get_post_type_labels(get_post_type_object(get_post_type($post))); $info = ' (current ' . $post_type_label->singular_name . ')'; } $res .= '<option data-post-type="' . get_post_type($post) . '" value="' . $post->ID . '"' . $selected . '>' . $level_string . $post->post_title . $info . '</option>'; if (is_post_type_hierarchical(get_post_type($post))) { $children = get_post_types_by_type(get_post_type($post), array(), array('post_parent' => $post->ID)); if (!empty($children)) { $level++; foreach ($children as $child) { $res .= wall_get_post_types_option($child, $id_selected, $current_post_id, $level); } } } return $res; }
$postypes = get_displayed_post_types(true); } global $post; if (!empty($postypes)) { ?> <div class="results"> <div class="column column-left"> <?php foreach ($postypes as $postype) { ?> <div class="post-type-selector" data-type="<?php echo $postype; ?> "> <?php $post_type_label = get_post_type_labels(get_post_type_object($postype)); echo $post_type_label->name; ?> </div> <?php } ?> </div> <div class="column column-right"> <?php foreach ($postypes as $postype) { $posts = get_posts(array('post_type' => $postype, "numberposts" => -1, "exclude" => $exclude)); ?> <div class="postype-section" data-type="<?php echo $postype; ?>
/** * @test */ function it_has_methods_for_setting_the_labels() { Builder::make('book')->setLabel('archives', 'All the Bookz')->setLabel('search_items', 'Find {many}')->setLabel('some_custom_label', 'BOOKMADNESS')->oneIs('Book')->manyAre('Books')->register(); $book = get_post_type_object('book'); $labels = get_post_type_labels($book); $this->assertSame('Book', $labels->singular_name); $this->assertSame('Books', $labels->name); $this->assertSame('All Books', $labels->all_items); $this->assertSame('Edit Book', $labels->edit_item); $this->assertSame('Find Books', $labels->search_items); $this->assertSame('BOOKMADNESS', $labels->some_custom_label); $this->assertSame('Add New Book', $labels->add_new_item); $this->assertSame('All the Bookz', $labels->archives); }
/** * Add action links to Stream drop row in admin list screen * * @filter wp_stream_action_links_{connector} * * @param array $links Previous links registered * @param object $record Stream record * * @return array Action links */ public static function action_links($links, $record) { if (in_array($record->context, array('downloads'))) { $links = WP_Stream_Connector_Posts::action_links($links, $record); } elseif (in_array($record->context, array('discounts'))) { $post_type_label = get_post_type_labels(get_post_type_object('edd_discount'))->singular_name; $base = admin_url('edit.php?post_type=download&page=edd-discounts'); $links[sprintf(__('Edit %s', 'stream'), $post_type_label)] = add_query_arg(array('edd-action' => 'edit_discount', 'discount' => $record->object_id), $base); if ('active' === get_post($record->object_id)->post_status) { $links[sprintf(__('Deactivate %s', 'stream'), $post_type_label)] = add_query_arg(array('edd-action' => 'deactivate_discount', 'discount' => $record->object_id), $base); } else { $links[sprintf(__('Activate %s', 'stream'), $post_type_label)] = add_query_arg(array('edd-action' => 'activate_discount', 'discount' => $record->object_id), $base); } } elseif (in_array($record->context, array('download_category', 'download_tag'))) { $tax_label = get_taxonomy_labels(get_taxonomy($record->context))->singular_name; $links[sprintf(__('Edit %s', 'stream'), $tax_label)] = get_edit_term_link($record->object_id, wp_stream_get_meta($record, 'taxonomy', true)); } elseif ('api_keys' === $record->context) { $user = new WP_User($record->object_id); if (apply_filters('edd_api_log_requests', true)) { $links[__('View API Log', 'stream')] = add_query_arg(array('view' => 'api_requests', 'post_type' => 'download', 'page' => 'edd-reports', 'tab' => 'logs', 's' => $user->user_email), 'edit.php'); } $links[__('Revoke', 'stream')] = add_query_arg(array('post_type' => 'download', 'user_id' => $record->object_id, 'edd_action' => 'process_api_key', 'edd_api_process' => 'revoke'), 'edit.php'); $links[__('Reissue', 'stream')] = add_query_arg(array('post_type' => 'download', 'user_id' => $record->object_id, 'edd_action' => 'process_api_key', 'edd_api_process' => 'regenerate'), 'edit.php'); } return $links; }
/** * Extends an existing post type object. Currently only handles labels. * * @param stdClass|WP_Post_Type $pto A post type object */ public function extend($pto) { # Merge core with overridden labels $this->args['labels'] = array_merge((array) get_post_type_labels($pto), $this->args['labels']); $GLOBALS['wp_post_types'][$pto->name]->labels = (object) $this->args['labels']; }
/** * Comparator for post_types string */ function custom_cmp_posttypes($post_type_1, $post_type_2) { $current_post_type_label_1 = get_post_type_labels(get_post_type_object($post_type_1)); $current_post_type_label_2 = get_post_type_labels(get_post_type_object($post_type_2)); return strcmp($current_post_type_label_1->name, $current_post_type_label_2->name); }
/** * Return a page title based on the current page. * * @return string */ function rock_get_the_page_title() { $title = ''; switch (true) { case is_front_page(): $title = get_the_title(get_option('page_on_front')); break; case is_home(): $title = get_the_title(get_option('page_for_posts')); break; case is_archive(): $title = get_the_archive_title(); break; case is_search(): $title = sprintf(esc_html_x('Search Results for: %s', 'search term', 'rock'), sprintf('<span>%s</span>', get_search_query())); break; case is_404(): $title = esc_html__('404 Page Not Found', 'rock'); break; case is_page(): $title = get_the_title(); break; case ($post = get_queried_object()) && !is_post_type_hierarchical(get_post_type($post)): $show_on_front = get_option('show_on_front'); $page_for_posts = get_option('page_for_posts'); if ('post' === $post->post_type && 'posts' !== $show_on_front && !empty($page_for_posts)) { $title = get_the_title($page_for_posts); break; } $labels = get_post_type_labels(get_post_type_object($post->post_type)); $title = isset($labels->name) ? $labels->name : false; break; } /** * Filter the page title. * * @since 1.0.0 * * @var string */ return (string) apply_filters('rock_the_page_title', $title); }