Greg Perham
Greg Perham

Reputation: 1857

How can I add custom fields to WordPress's built-in taxonomy meta boxes?

Specifically, I want to add a couple fields to the Categories meta box ('categorydiv'). I was hoping there would be a filter, but I couldn't find one. I know I can create a custom meta box, but it will be easiest for my client if they see these fields tied to choosing a category. The visual break of a new meta box is undesirable, and it would be unacceptable to have it appear anywhere but immediately below Categories (e.g., under Tags).

Upvotes: 0

Views: 307

Answers (1)

Greg Perham
Greg Perham

Reputation: 1857

There is no action/filter hook to customize the output of WP's taxonomy meta boxes. There are two options: replace the Categories meta box with a custom version, or move your meta fields into the Categories meta box with JavaScript.

I added my custom meta boxes to the Categories div in 5 minutes with jQuery. I prefer this version because it's short, simple, and in most cases more versatile.

Do add_meta_boxes as normal, then add another function to jQuery it up:

function my_meta_box_customization() {
    ?>
    <script>
        (function($) {
            $(function(){
                $('#my-meta-box-div-id').find('.inside').appendTo('#categorydiv')
                    .end().end().remove();
            });
        })(jQuery);
    </script>
    <?php
}
add_action( 'admin_head', 'my_meta_box_customization' );

I like to throw a <style> tag in there, too.

I'm not going to bother with all the code for replacing the Categories meta box entirely, but the general idea would be:

  1. remove_meta_box( 'categorydiv', 'post', 'side' );
  2. add_meta_boxes with a function that loads post_categories_meta_box into a variable and customizes it with regex or some such.
  3. Mess around with adding, removing and re-adding meta boxes to get the desired order (e.g, Categories under Publish, above Tags).

It would be a hassle, and I'm not recommending it.

Upvotes: 1

Related Questions