Description
Version 1.1 brings support for programmatically registering CPT-onomies. While the majority of users will be fine using the settings panel to register their CPT-onomies, this functionality will come in handy for a few.
The custom post type must be registered BEFORE you register it’s namesake CPT-onomy! I recommend hooking into the ‘wp_loaded’ action to register your CPT-onomy.
This function belongs to the ‘CPT_ONOMIES_MANAGER’ class, which is initiated and assigned to the $cpt_onomies_manager variable. The function allows you to pass certain arguments to customize your CPT-onomy:
- ‘label’ (string) – Name of the CPT-onomy shown in the menu. Usually plural. If not set, the custom post type’s label will be used.
- ‘labels’ (array) – An array of labels for this CPT-onomy. You can see accepted values in the function get_taxonomy_labels() in ‘wp-includes/taxonomy.php’. By default, tag labels are used for non-hierarchical types and category labels for hierarchical ones. If not set, will use WordPress defaults.
- ‘public’ (boolean) – If the CPT-onomy should be publicly queryable. If not set, defaults to custom post type’s public definition.
- ‘has_cpt_onomy_archive’ (boolean) – Sets whether the CPT-onomy will have an archive page. Defaults to true.
- ‘cpt_onomy_archive_slug’ (string) – The slug for the CPT-onomy archive page. ‘has_cpt_onomy_archive’ must be true. Accepts variables $post_type, $term_slug and $term_id in string format as placeholders. Default is ‘$post_type/tax/$term_slug’.
- ‘restrict_user_capabilities’ (array) – User roles who have capability to assign CPT-onomy terms. If empty, ALL user roles will have the capability. Default is array( ‘administrator’, ‘editor’, ‘author’ ).
Usage
$cpt_onomies_manager->register_cpt_onomy( string $cpt_onomy, string|array $object_type, string|array $args );
Parameters
$cpt_onomy (string) (required)
Default: none
Name of the custom post type/CPT-onomy.
$object_type (string|array) (required)
Default: none
Name of the object type, or post type, for which you're "attaching" your CPT-onomy.
$args (string|array) (optional)
Default: none
Arguments used to customize the CPT-onomy.
Return Values
(null) Returns early if taxonomy already exists or if post type does not exist.
Examples
<?php
add_action( 'wp_loaded', 'my_website_register_cpt_onomy' );
function my_website_register_cpt_onomy() {
global $cpt_onomies_manager;
if ( $cpt_onomies_manager ) {
$cpt_onomies_manager->register_cpt_onomy( 'actors', 'movies', array( 'restrict_user_capabilities' => array( 'administrator' ) ) );
}
}
?>
Notes
- Added in version 1.1.
