7

Important Update: Register Taxonomies And Post Types With Labels

There's been a somewhat "big" update, or change, made to the register_taxonomy() and register_post_type() functions in Wordpress. You will now be able to customize the capabilities of custom types you create, even MORE!! Note: This is a new update, which may be subject to change in the near future since it is more than a minor typical alteration....
Important Update: Register Taxonomies And Post Types With Labels

What's the news

There was an important change (see #12968) made recently that has been committed to SVN. This new update has to do with the way you create new custom taxonomies and post types. Also see ticket #13357 as well for more info.

This update allows a post type or taxonomy to register a series of custom labels, using the labels array. This was made to be backwards compatible, so you the singular_label still exists, it is just ignored if not used, and used if it is used. The label and singular_label properties were left alone so they technically still exist as they always have.

Using the new labels, or $labels array argument there are several labels you can create for a particular post type or taxonomy. You would set a $labels variable to equal the array of label options you wish to use, if any, and then use that variable as the value for the key "labels" in the array which you set up when registering a new taxonomy or post type.

For Example: How You Would Use Labels

Here's what I mean, it's really quite simple to do, and it's organized nicely so you don't get all confused by the 3D array insanity that was possible.

So first you need to create the variable and array of desired labels.


  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name' => _x( 'Genres', 'taxonomy general name' ),
    'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Genres' ),
    'popular_items' => __( 'Popular Genres' ),
    'all_items' => __( 'All Genres' ),
    'parent_item' => __( 'Parent Genre' ),
    'parent_item_colon' => __( 'Parent Genre:' ),
    'edit_item' => __( 'Edit Genre' ),
    'update_item' => __( 'Update Genre' ),
    'add_new_item' => __( 'Add New Genre' ),
    'new_item_name' => __( 'New Genre Name' ),
  ); 	

Then thisis how you'd register taxonomies, or post types to use the above $labels variable.


  register_taxonomy('genre',array('book'), array(
    'hierarchical' => true,
    'labels' => $labels, // SEE this is the $labels array from above
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'genre' ),
  ));

This is really nothing too complicated I hope if you already have a familiarity with register taxonomies or post types. (I just lost my train of thought cause I just slammed my knee into the desk, OW!) Okay, so if you are unclear about the above example, refer to the WordPress Codex - Function Reference for the functions register_post_type() and register_taxonomy() to learn more.

Possible Labels Explained In English

This update basically removes the need to use edit_cap, edit_type_cap, delete_cap, etc. They are are all gone (though still there for backwards compatibility). The function capability_type (i.e. "post", "page", "mycustomtyp" etc.), property is now what does the managing of Capabilities, and after which then builds the corresponding capabilities.

Below is a quick view of the optional labels which can be used in the array as it has been documented for both taxonomies and post types alike.


/*
 * labels
 *		(array) (optional) labels - An array of labels for this taxonomy. By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.

 		* Default: if empty, name is set to label value, and singular_name is set to name value 

		* 'name' - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is _x( 'Post Tags', 'taxonomy general name' ) or _x( 'Categories', 'taxonomy general name' ). When internationalizing this string, please use a gettext context matching your post type. Example: _x('Writers', 'taxonomy general name');

		* 'singular_name' - name for one object of this taxonomy. Default is _x( 'Post Tag', 'taxonomy singular name' ) or _x( 'Category', 'taxonomy singular name' ). When internationalizing this string, please use a gettext context matching your post type. Example: _x('Writer', 'taxonomy singular name');

		* 'search_items' - the search items text. Default is __( 'Search Tags' ) or __( 'Search Categories' )

		* 'popular_items' - the popular items text. Default is __( 'Popular Tags' ) or __( 'Popular Category' )

		* 'all_items' - the all items text. Default is __( 'All Tags' ) or __( 'All Categories' )

		* 'parent_item' - the parent item text. This string is not used on non-hierarchical taxonomies such as post tags. Default is null or __( 'Parent Category' )

		* 'parent_item_colon' - The same as parent_item, but with colon : in the end null, __( 'Parent Category:' )

		* 'edit_item' - the edit item text. Default is __( 'Edit Tag' ) or __( 'Edit Category' )

		* 'update_item' - the update item text. Default is __( 'Update Tag' ) or __( 'Update Category' )

		* 'add_new_item' - the add new item text. Default is __( 'Add New Tag' ) or __( 'Add New Category' )

		* 'new_item_name' - the new item name text. Default is __( 'New Tag Name' ) or __( 'New Category Name' )
 *
 */

Get more information

If you want to get some more info on this new update, you could check out the thread ON WPdevel where Andrew Nacin provides bit more in-depth explanation about it. It definitely seems like a great update which expands the customizability, and overall capabilities you can have with the awesomely implemented new post type feature.

Have you worked with custom post types or custom taxonomies yet with WordPress 3.0 alpha or beta?? I would like to know what you like or dislike about the new feature so leave a comment below and let me know.

Just an FYI:

The new date for the WordPress 3.0 release is now set for "June 2010, early to mid-month, hopefully". So keep an eye out for it, (or the next beta release lol). I know I am excited for the released, and have really enjoyed just playing with the beta versions. So the official release will be great I'm sure.

Shortlink:

Get automatic updates! Subscribe to Our RSS Feed or Get Email Updates sent straight to your inbox!

About the Author

Jared is from Boston working as a web and graphic designer. Also owns the design blog Tweeaks.com, and has designed many other websites powered by Wordpress including the New2WP theme.

Level: Pro

User Comments

( ADD YOURS )

  1. I haven't worked with these yet, but it's a capability important enough to me that I considered hacking core to implement it for myself. Fortunately, WP is stepping up to deliver, no need for me to go into core right now.

    Still, this looks like a few days work to get a grip on it all. Probably a smart thing to get started with sooner than later.


  2. Since WP 3.0 alpha was available for download, about 2-3 months ago, I've began digging through the core files which I never really understood the reason behind why that would be worth my time to do. Then I saw a chunk of code that I had recognized using before and I suddenly realized. Plugins, themes, hacks, code snips, and anything you may add to functions.php is all based on the functions within the core, and understanding how they all work is well worth knowing if you learn anything at all about WordPress. It's been a great learning experience for me.

    I definitely agree on the sooner rather than later, and do encourage the sooner part too as someone who recently learned that first hand.

    Oh, and the register_post_types() and register_taxonomies() functions are really not that complicated. They are explain in the codex, and there is a quickly growing number of articles about using them too.

  1. Avatar

    Your Name
    May 17


    CommentLuv badge