I’m registering 7 custom taxonomies in my functions.php. Since each function uses the same labels and arguments and is attached to the same Custom Post Type, I’m wondering if it’s possible to register all the taxonomies in 1 function? Here is the code that is repeated 7 times with the <XXX>
as the only changing parts:
function custom_taxonomy_<XXX> () {
$labels = array(
'name' => '<XXX>',
'singular_name' => '<XXX>',
'menu_name' => '<XXX>s',
'all_items' => 'All <XXX>s',
'parent_item' => 'Parent <XXX>',
'parent_item_colon' => 'Parent <XXX>:',
'new_item_name' => 'New <XXX> Name',
'add_new_item' => 'Add New <XXX>',
'edit_item' => 'Edit <XXX>',
'update_item' => 'Update <XXX>',
'separate_items_with_commas' => 'Separate <XXX> with commas',
'search_items' => 'Search <XXX>s',
'add_or_remove_items' => 'Add or Remove <XXX>s',
'choose_from_most_used' => 'Choose from the most used <XXX>s',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
);
register_taxonomy( '<XXX>', 'custom_post_type', $args );
}
add_action( 'init', 'custom_taxonomy_<XXX>', 0 );
Nope. Not the WordPress way at least.
You could probably make helper function (plain PHP realm in other words) to build instances of label arrays, however that would seriously hinder making strings translatable.
I would try to create a wrapping function that can provide values for all those
XXX's
at runtime. Here’s a rough, untested example:You’d need to finish the implementation here, but the idea is to keep the number of necessary parameters to a minimum while still populating the entire
$labels
array with the appropriate values.You may end up with the
build_and_register_custom_taxonomy
function accepting 8 arguments, or perhaps just the three or four.You could also override portions of the
$args
array by passing down arguments from the parentbuild_and_register_custom_taxonomy
function as needed.Good luck!