How to Change Default Tag Meta Box: Make It like Category Meta Box

Do you want to display all post tags at once on the post edit page? I will show you how to make this replacement in two easy steps.

#taxonomies, #wp-admin  /   28

Let me show you what I mean:

Tags like categories on the post edit page

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.

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Related Posts

Comments 28

← Older
  • 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

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email