Add Field to Category / Taxonomy WordPress

I recently had a need to store some extra meta information on each category / custom taxonomy in WordPress. Basically, I just needed to add an extra field. It was just a simple matter of finding the right hooks and making a simple category meta plugin. We don’t do a whole lot of adding new categories, we mainly just needed to store some additional information on the existing ones, so I just hooked into the edit form.

I also wanted this to function without using any custom tables, but there is really no good place to store information of this sort within the default WordPress tables. I decided that the wp_options table would be best suited since when it comes down to it, most sites wouldn’t have more than a couple of hundred categories. There isn’t enough information being stored on our end for this to create a problem.

add_action('edited_category', 'taxonomy_meta_form');

The first action hooks in category/taxonomy edit form. It displays the form we have created and output from the function called taxonomy_meta_form_fields. The second action calls taxonomy_meta_form when the category form is actually edited.

Full Code

add_action('edited_category', 'taxonomy_meta_form');

function taxonomy_meta_form() {
	if ( !current_user_can( 'manage_options' ) ) //make sure user has appropriate permissions

		$extra_title = sanitize_text_field($_POST['extra_title']); //make sure nothing malicious
		$extra_titles = get_option('extra_taxonomy_titles');
		$extra_titles = unserialize($extra_titles);

		$cat_ID = (int)$_POST['tag_ID'];

		$extra_titles[$cat_ID] = $extra_title;   //update the value for this category's ID

		update_option('extra_taxonomy_titles', $extra_titles);  //store the array, WP handles the serialization

function taxonomy_meta_form_fields ($tag) {
	$cat_ID = $tag->term_id;
	$extra_titles = get_option('extra_taxonomy_titles');
	$extra_title = $extra_titles[$cat_ID];
	<tr class="form-field">
	<th valign="top" scope="row">
	<label for="extra_title"><?php _e('Extra Title', ''); ?></label>
	<input type="text" name="extra_title" size="100" value="<?php echo $extra_title; ?>">

Hooking into Custom Taxonomies

add_action('edited_TAXONOMYNAME', 'taxonomy_meta_form');

To do this same action, but for custom taxonomies instead, its a simple matter of changing the action hook. Just replace TAXONOMYNAME above with the slug that is associated with your custom taxonomy.

Displaying the Information

To retrieve the information stored for us on the frontend, I’d recommend creating a simple function for store in your theme’s functions.php file:

function tj_extra_title( $id){
	$extra_titles = get_option('extra_taxonomy_titles');
	$extra_titles = unserialize($extra_titles);
	$extra_title = $extra_titles[$id];

	return $extra_title;

Then, it could be called from a template page as follows:

$extra_title = tj_extra_title( $category->term_id );  //replace the id with whatever variable is appropriate for your context
echo $extra_title;

View code on github. Ask questions below.

Posted by Will on November 9, 2012

Leave a Reply

Your email address will not be published. Required fields are marked *

TechJunkie Also Recommends