WordPress 3.5 is here!! If you haven’t played with it yet, go get it!
I’m proud to be listed as a contributor on this release and want to highlight a new feature geared towards developers that I helped work on. If you have checked the register_taxonomy() codex page recently, you may have noticed a new optional argument, ‘show_admin_column.’
From the codex:
show_admin_column
(boolean) (optional) Whether to allow automatic creation of taxonomy A taxonomy is a way to group things together. In WordPress, some common taxonomies are category, link, tag, or post format. https://codex.wordpress.org/Taxonomies#Default_Taxonomies. columns on associated post-types.
Default: false
As stated, this new argument allows easy registration of taxonomy columns on post (and custom post type WordPress can hold and display many different types of content. A single item of such a content is generally called a post, although post is also a specific post type. Custom Post Types gives your site the ability to have templated posts, to simplify the concept.) list table screens much like the default tags’ and categories’ columns.
This should make plugin A plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. These can be free in the WordPress.org Plugin Directory https://wordpress.org/plugins/ or can be cost-based plugin from a third-party and theme developers happy as they’ll no longer need to build these columns manually.
The new argument in action:
// hook into the init action and call create_activity_taxonomies when it fires
add_action( 'init', 'create_activity_taxonomies' );
// create taxonomy, "fitness-type" for the post type "activity"
function create_activity_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Fitness Types', 'taxonomy general name' ),
'singular_name' => _x( 'Fitness Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Fitness Types' ),
'all_items' => __( 'All Fitness Types' ),
'parent_item' => __( 'Parent Fitness Type' ),
'parent_item_colon' => __( 'Parent Fitness Type:' ),
'edit_item' => __( 'Edit Fitness Type' ),
'update_item' => __( 'Update Fitness Type' ),
'add_new_item' => __( 'Add New Fitness Type' ),
'new_item_name' => __( 'New Fitness Type Name' ),
'menu_name' => __( 'Fitness Type' ),
);
register_taxonomy( 'fitness-type', array( 'activity' ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'fitness-type' ),
) );
}
Another key addition is the “manage_taxonomies_for_{$post_type}_columns
” filter Filters are one of the two types of Hooks https://codex.wordpress.org/Plugin_API/Hooks. They provide a way for functions to modify data of other functions. They are the counterpart to Actions. Unlike Actions, filters are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output.. This allows developers to add a taxonomy column to a post type outside of the register_taxonomy()
function:
Example:
add_filter( 'manage_taxonomies_for_activity_columns', 'activity_type_columns' );
function activity_type_columns( $taxonomies ) {
$taxonomies[] = 'activity-type';
return $taxonomies;
}
#3-5, #admin-columns, #dev-notes, #register_taxonomy, #taxonomy
You must be logged in to post a comment.