Today, I was working on a client project and needed to add a little snippet underneath a hierarchical taxonomy form that was registered on a custom post type. Unfortunately, after spending about ten minutes digging through core and googling, I couldn’t find an action hook to use. Luckily, after tracing the output a little further back, I found what I was looking for.
When registering a custom taxonomy, one of the oft forgotten arguments is meta_box_cb
, or the callback function used to render the meta box in the admin edit screen. Tracing this back a little further, the default value is set in wp-includes/taxonomy.php on Line 454
. Using one of those default functions within our custom callback, we can essentially add as much code before or after the default taxonomy form.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
register_taxonomy( 'acme_custom_taxonomy', array( 'my_cpt', 'post' ), array( | |
'meta_box_cb' => 'acme_metabox_cb', | |
) ); | |
function acme_metabox_cb( $post, $box ){ | |
// Render the default hierarchical meta box form | |
post_categories_meta_box( $post, $box ); | |
// Add all of our new stuff! | |
echo "<p>This will show up below the checkboxes that you are used to!</p>"; | |
} |
It’s that simple! If you need to add something to the non-hierarchical taxonomy form, simply swap out line 9 for the post_tags_meta_box()
function instead.