How to Change Default Tag Meta Box: Make It like Category Meta Box
Let me show you what I mean:

As you can see the new meta box displays all tags at once. Why do we need this replacement? Dunno, maybe it is more usable for some of us. Also you can do more things with jQuery.
I will show you how to make this replacement in two easy steps. Just insert the code from the first and the second step into your functions.php
file and everything should work.
Tags which are already attached to posts will be saved, multi tag selecttion is supported.
Step 1. Remove the Old Tags Meta Box
To create the new one, we need to destroy the old one. Just because WordPress doesn’t support filters and actions which allow to change the metabox content. Also we must not change the WordPress core files.
/*
* Meta Box Removal
*/
function rudr_post_tags_meta_box_remove() {
$id = 'tagsdiv-post_tag'; // you can find it in a page source code (Ctrl+U)
$post_type = 'post'; // remove only from post edit screen
$position = 'side';
remove_meta_box( $id, $post_type, $position );
}
add_action( 'admin_menu', 'rudr_post_tags_meta_box_remove');
Step 2. Create the New One, But Looking Like the Categories Meta Box
I use only 3 WordPress functions:
add_meta_box()
,get_terms()
— to get all existing tags,get_the_terms()
— to get all tags assigned to post.
This is the code:
/*
* Add
*/
function rudr_add_new_tags_metabox(){
$id = 'rudrtagsdiv-post_tag'; // it should be unique
$heading = 'Tags'; // meta box heading
$callback = 'rudr_metabox_content'; // the name of the callback function
$post_type = 'post';
$position = 'side';
$pri = 'default'; // priority, 'default' is good for us
add_meta_box( $id, $heading, $callback, $post_type, $position, $pri );
}
add_action( 'admin_menu', 'rudr_add_new_tags_metabox');
/*
* Fill
*/
function rudr_metabox_content($post) {
// get all blog post tags as an array of objects
$all_tags = get_terms( array('taxonomy' => 'post_tag', 'hide_empty' => 0) );
// get all tags assigned to a post
$all_tags_of_post = get_the_terms( $post->ID, 'post_tag' );
// create an array of post tags ids
$ids = array();
if ( $all_tags_of_post ) {
foreach ($all_tags_of_post as $tag ) {
$ids[] = $tag->term_id;
}
}
// HTML
echo '<div id="taxonomy-post_tag" class="categorydiv">';
echo '<input type="hidden" name="tax_input[post_tag][]" value="0" />';
echo '<ul>';
foreach( $all_tags as $tag ){
// unchecked by default
$checked = "";
// if an ID of a tag in the loop is in the array of assigned post tags - then check the checkbox
if ( in_array( $tag->term_id, $ids ) ) {
$checked = " checked='checked'";
}
$id = 'post_tag-' . $tag->term_id;
echo "<li id='{$id}'>";
echo "<label><input type='checkbox' name='tax_input[post_tag][]' id='in-$id'". $checked ." value='$tag->slug' /> $tag->name</label><br />";
echo "</li>";
}
echo '</ul></div>'; // end HTML
}
You can use this code not only for the tags but also for any non-hierarchical taxonomy.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hello
This is amazing and just what I was looking for – works great!. I have two more custom post types I’d like to effect and I have been trying to add them as an array:
But nothing I try works. I’m looking to change the Tag meta box for all three of my post types. Any ideas? Really stuck now : [
Hello, Gordon,
add_meta_box()
function supports only string as$post_type
parameter :)Try to use it three times, for each post type separately.
A hah – I thought there might be a neater way, but yes I repeated the entire code three times replacing the post type annd calling your functions rudr, rudr2 and rudr3… thanks for the tip!
Hello Miriam,
The first thing that I can suggest you is to make the following replacements in your code:
Replace this line
with:
You’re not working with post tags! You’re working with the custom taxonomy. So, the next replacement is
to
I hope it helps.
Hello
thanks for this great tutorial!
I would like to use this to setup metabox to show only 3 specific tags:
Featured / Promoted / Ticker
So user can chose one or all of this three tags.
Regards :)
Hello
do you think its ready for the newest version, 4.6.3
By pasting your code in my functions.php i see only the Boxes but no text.
When i turn on wp_debug here are the error messages:
Notice: Trying to get property of non-object in \test.de\wp-content\themes\twentysixteen\functions.php on line 515
Notice: Trying to get property of non-object in \test.de\wp-content\themes\twentysixteen\functions.php on line 518
Notice: Trying to get property of non-object in \test.de\wp-content\themes\twentysixteen\functions.php on line 520
these are the lines 515-520:
Hello,
maybe there are no tags on your website?
thanks for your fast reply!
Yes there are…
But
I have tested now with an clean installation and there it works fine.
So the fault is on my side
Before making this check:
if ( in_array( $tag->term_id, $ids ) ) {
try to look what is$tag
, just with this line of code:print_r( $tag );
Hi, thanks for the code.
I want to implement it in the frontend, so that a user when publishing a post chooses the existing tags.
Could you implement it?
Sorry for my english
Hi Roland,
no, I have no ready code for this but if you want to allow users to select multiple tags + search by them, I recommend you to look at Select2 jQuery plugin.
hi,
can you please show the reverse situation? from category to tag?
Hi,
I have no ready code for that
Hi Misha
Great tutorial, everithing worked fine
I was wandering if u know hot to add an ‘Add New’ button to the new metabox, just like it is in category mbox
and also a search bar to filter the list
Hi,
Thank you!
I have some ideas, but I don’t have ready code, sorry. Try to inspect how it is implemented for category metabox.
I solved my first doubt. I wanted to change the metabox af a non hirerachical custom taxonomy, and it is really easy. Just add this line in the $args when registering it.
For reference:
https://gist.github.com/gschoppe/29ba81a1f676d7802cb8#file-cat-like-custom-taxonomy-php
https://gist.github.com/hlashbrooke/5983859
——
I’m still looking forword finding a way to add a search bar wich filters the checkbox list. I’ll keep u informed if i find out.
Great! Thank you for the info.
Found a perfect plugin for filtering categories
https://it.wordpress.org/plugins/admin-category-filter/
Hi, thanks for this code!
When I add the first part to remove the existing box, that works fine. But when I paste in the second part to add the new box, my site throws a 500 error. Any idea why this might be happening?
Hi Cory,
Turn on
WP_DEBUG
in your wp-config.php file and let me know what error message you see.Hi! Sorry I’m so late to reply here…I set it to send me notices when somebody replies but I must have missed it!
In any case, the debug check found the problem for me and all works well now. Thanks again for this great code!
There should be a way to do this by using a child theme functions.php without having to delete the original code.
Of course, you can use the above code in your child theme.
Well thanks for the reply. Appreciated. I added both remove and add filter codes to the child theme with no results on the tags metabox. I am using beaver builder with themer theme, plus a custom post type. Are you familiar with Beaver builder? Would you expect this to work ok?
Thank-you.
I never work with builders.
Ok, fair comment. I’ll try on a clean install.
Thank-you
Is there a way to make the metabox scrollable instead of as long as the list of terms within?
Actually I figured it out, use this code for the Metabox content
Hi Misha,
I recently discovered your blog and thank you for this great source of kwnoledge you shared here.
I’m a graphist and not a php/ajax developper so excuse me if i’m asking basic question. I was wandering how can I use the translated/localized WP string of the header Tag Metabox? It should be on this line I guess but…
Thx also to Marc for his improvment.
Hi Bruno,
Do you mean something like this?
$heading = __( 'Tags' );
Yes, It works like a charm and displays the word Tags in localized language.
thank you
Hi again, Oups second question
As I also use Tags for pages too, with these scripts :
How can I display the tag Metabox to pages too ?
Thank you very much
Hi Bruno,
Just set
$post_type = 'page';
in my code and that should be enough.Sorry but not enough I guess.
Addin this line remove the meta-box from articles and duplicate one in pages admin dashboard. But may be I don’t place it at the right place. (adding line 9 in your code).
this feature is not a big point to me but if I can learn something it would be great.
Thank you anyway
Bruno
thanks