Skip to content

Instantly share code, notes, and snippets.

@MatthewEppelsheimer
Last active March 4, 2016 16:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewEppelsheimer/5793409 to your computer and use it in GitHub Desktop.
Save MatthewEppelsheimer/5793409 to your computer and use it in GitHub Desktop.
WordPress register_taxonomy() boilerplate
// Prepare post types to associate taxonomy with
// This will only return public post types (excluding for example attachments, revisions, and nav_menu_items)
$object_types = get_post_types( array( 'public' => true ), 'names' );
// Prepare args to register taxonomy
$args = array(
'label' => _x( 'Objects', 'object taxonomy label' ) // plural label
, 'labels' => array(
'name' => _x( 'Objects', 'object taxonomy name' ) // general name, usually plural
, 'singular_name' => _x( 'Object', 'object taxonomy singular name' ) // singlar name for object
, 'menu_name' => _x( 'Objects', 'object taxonomy menu name' ) // menu name text, defaults to name
, 'all_items' => _x( 'All Objects', 'object taxonomy') // all items text
, 'edit_item' => _x( 'Edit Object', 'object taxonomy') // edit item text.
, 'view_item' => _x( 'View Object', 'object taxonomy' ) // view item text
, 'update_item' => _x( 'Update Object', 'object taxonomy' ) // update item text
, 'add_new_item' => _x( 'Add New Object', 'object taxonomy' ) // add new item text
, 'new_item_name' => _x( 'New Object Name', 'object taxonomy' ) // new item name text
, 'search_items' => _x( 'Search Objects', 'object taxonomy' ) // search items text
, 'popular_items' => _x( 'Popular Objects', 'object taxonomy' ) // popular items text
// Include only for hierarchical taxonomies
, 'parent_item' => _x( 'Parent Object', 'object taxonomy parent object' ) // parent item text
, 'parent_item_colon' => _x( 'Parent Object:', 'object taxonomy parent object with colon' ) // same as parent_item, but with colon : in the end
// Include only for non-hierarchical taxonomies
, 'separate_items_with_commas' => _x( 'Separate objects with commas', 'object taxonomy separate with commas' ) // separate item with commas text used in the taxonomy meta box
, 'add_or_remove_items' => _x( 'Add or remove objects', 'object taxonomy add or remove' ) //add or remove items text and used in the meta box when JavaScript is disabled
, 'choose_from_most_used' => _x( 'Choose from the most used objects', 'object taxonomy choose from most used' ) // choose from most used text used in the taxonomy meta box
, 'not_found' => _x( 'No objects found.', 'object taxonomy none found message' ) // text displayed via clicking 'Choose from the most used tags' in the taxonomy meta box when no tags are available
)
, 'public' => true // should be exposed in admin UI? Default: true
, 'show_ui' => true // generate default IU? Default: 'public' value
, 'show_in_nav_menus' => true // make available to select in nav menus. Default: 'public' value
, 'show_tagcloud' => true // all the Tag Cloud widget to use this taxonomy. Default: 'show_ui' value
, 'show_admin_column' => false // automatically add taxonomy columns on associated post-types. Default: false
, 'hierarchical' => false // is hierarchical? Default: false
, 'update_count_callback' => null // Function name called when the count of an associated post object type is updated. Works much like a hook. Default: none
// The query_var is used for direct queries through WP_Query like `new WP_Query( array( 'people'=>$person_name ) )`
// and URL queries like `/?people=$person_name`. Setting query_var to false will disable these methods, but you can
// still fetch posts with an explicit WP_Query taxonomy query like `WP_Query( array( 'taxonomy'=>'people', 'term'=>$person_name ) )`.
, 'query_var' => 'taxonomy_name' // False to disable the query_var, or set a string as a custom query_var. Default: $taxonomy_name
, 'rewrite' => array(
'slug' => 'object_name' // permalink string. Default: taxonomy name slug
, 'with_front' => true // allow permalinks to be prepended with front base. Default: true
, 'hierarchical' => false // allow hierarchical urls. Default: false
// see: http://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/
, 'ep_mask' => EP_NONE // Default: EP_NONE
)
, 'capabilities' => array(
// Default: none
/* Uncomment to use
'manage_terms' => 'manage_objects'
, 'edit_terms' => 'manage_objects'
, 'delete_terms' => 'manage_objects'
, 'assign_terms' => 'edit_posts'
*/
)
, 'sort' => false // whether the taxonomy should remember order in which terms are added as objects. Default: none
);
// Register the taxonomy with prepared $object_types and $args
register_taxonomy( 'taxonomy_name', $object_types, $args );
// To be safe, do this next to make sure post types are attached inside filter callbacks during `parse_request` and `pre_get_posts`
foreach ( $object_types as $type ) {
register_taxonomy_for_object_type( 'status', $type );
}
@tangrufus
Copy link

what is the use of register_taxonomy_for_object_type( 'status', $type ) ?

I don't get the meaning from line 86 comment.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment