Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Admin / Custom Taxonomy / Add Term or Taxonomy Meta Data

Add Term or Taxonomy Meta Data

by ShibaShake 30 Comments

Since WordPress 2.9, support has been added for term or taxonomy meta-data. The relevant functions are in wp-includes/meta.php and they include –

  • add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false)
  • update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = ”)
  • delete_metadata($meta_type, $object_id, $meta_key, $meta_value = ”, $delete_all = false)
  • get_metadata($meta_type, $object_id, $meta_key = ”, $single = false)

These functions are similar to the add_post_meta, update_post_meta, delete_post_meta, and get_post_meta functions that allow us to add meta data to post objects.

The generalized functions above will allow you to add meta data to any object type (including tags, categories, and any taxonomy object), as long as you create a relevant database table to store the information.

1. Meta-Data Type

First, we must decide what object type we want to assign meta-data to.

For example –

  • Post meta-data has type = post,
  • Comment meta-data has type = comment,

and so forth.

Here, we want to add meta-data to taxonomy objects, therefore we will set our

type = shiba_term.

2. Create a Taxonomy Table

Now, we must create a table to store the meta-data for our taxonomy objects. Meta-data tables should contain at least 4 columns with the following names –

  1. meta_id – Contains the unique id of each piece of meta-data information that is stored in our taxonomy meta-data table.
  2. {$type}_id – Contains the unique id of the object to associate with the given meta-data. In this article, our $type = shiba_term, therefore this column will be called shiba_term_id.
  3. meta_key – Meta-data key.
  4. meta_value – Meta-data value.

// create table in your plugin activation function
 function create_metadata_table($table_name, $type) {
	global $wpdb;

	if (!empty ($wpdb->charset))
		$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
	if (!empty ($wpdb->collate))
		$charset_collate .= " COLLATE {$wpdb->collate}";
			
	  $sql = "CREATE TABLE IF NOT EXISTS {$table_name} (
	  	meta_id bigint(20) NOT NULL AUTO_INCREMENT,
	  	{$type}_id bigint(20) NOT NULL default 0,
	
		meta_key varchar(255) DEFAULT NULL,
		meta_value longtext DEFAULT NULL,
		   		
	  	UNIQUE KEY meta_id (meta_id)
	) {$charset_collate};";
	
	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	dbDelta($sql);
}

We can set our table name to anything we want, but it is best to follow WordPress convention and name our taxonomy table as follows

global $wpdb;
$table_name = $wpdb->prefix . $type . 'meta';

Therefore, our table name in this example is wp_shiba_termmeta.

3. Register Taxonomy Table

To enable the WordPress metadata functions, we must also register our new table.

// Add to your plugin init function
global $wpdb;
$variable_name = $type . 'meta';
$wpdb->$variable_name = $table_name;

In particular, the meta-data functions will look for our table name in the variable –
$type . ‘meta’ within the $wpdb global object. Therefore, it is important to initialize this value correctly as we have shown above.

Since our $type = shiba_term, our table registration statement will look like this –

$wpdb->shiba_termmeta = $wpdb->prefix . 'shiba_termmeta'

We are Done!

And just like that, we are all done. We can now add meta-data values to any taxonomy object. For example, suppose to want to add some meta-data into our WordPress tags.

<?php
// Add to your plugin admin_init function
add_action ( 'edit_tag_form_fields', 'tag_input_metabox' );
add_action ( 'edited_terms', 'save_tag_data' );

function tag_input_metabox($tag) {
		
	$selected = get_metadata('shiba_term', $tag->term_id, 'new_metadata', TRUE);	
	
	?>
	<tr class="form-field">
		<th scope="row" valign="top"><label for="tag_widget"><?php _e('New Input') ?></label></th>
		<td>
		<select name='tag_meta_input' id='tag_meta_input'>	
		<!-- Display all our meta-data options -->
		</select>  
		 </td>
	</tr>
	
	<?php
}

function save_tag_data($term_id) {
        if (isset($_POST['tag_meta_input'])) {
	        $tag_metadata = esc_attr($_POST['tag_meta_input']);
	        update_metadata('shiba_term', $term_id, 'new_metadata', $tag_metadata);
        }
}
?>

tag_input_metabox adds a new input form to our WordPress Tag Edit screen. The input results are saved in the tag_meta_input field.

save_tag_data retrieves the input data from our new tag_meta_input field, and saves it to our tag object.

Leave a Reply Cancel reply

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

Comments

« Previous 1 2
  1. Al says

    May 23, 2014 at 1:59 am

    Hello Shiba,

    Great tutorial.

    Is that anyway we sort taxonomy term by meta_value? or can I retrieve taxonomy terms data using get_terms where $args = array (‘orderby’ => $meta_key or $meta_value)?

    Thanks very much,

    -Al
    Select term_id from taxonomy_term table

    Reply
  2. Karen says

    August 22, 2013 at 11:44 am

    Hi Shiba,

    I’m afraid I’ve never written a WordPress plugin, but if I understand your post correctly, it sounds like the setup for exactly what I hope to do: Beyond “Name,” “Slug,” and “Description,” there are some additional custom properties that I’d like to be able to specify for the tags on my (customized, self-hosted) WordPress site. So I’d like to add some custom input fields to the “Add New Tag” interface.

    In the hopes that your sample code would make just such a new custom field, I familiarized myself with the basic structure of a plugin (including registering activation function and then calling the admin-init function), and I built your sample code into what I think should be a working plugin. (I even retained “shiba_term” as the value of $type–figuring I could start customize the names after I got it working). Installing and activating it doesn’t create a new field, though–I’m sure because I’m doing something wrong and don’t know it!

    I’m certain I’m being a pest by replying to your lovely tutorial with the ultimate newbie question, but would much appreciate any guidance you can offer–even so much as pointing me to a good source where I can pick up the basics of writing/implementing plugins?

    Please do email me if you can spare the time and patience, and I can send you what I put together (I tried to paste it in this comment but I see that doesn’t go through).

    Many thanks!!! (And what beautiful artwork throughout your site–really counteracts the stress I usually feel when grappling with code!)

    🙂

    Reply
    • ShibaShake says

      August 23, 2013 at 8:49 pm

      I think there are some plugins that provide some higher level support for adding metadata to custom taxonomies.
      http://wordpress.org/plugins/taxonomy-metadata/

      I haven’t used the plugin so I don’t have any details, but it (or others like it) might be a good starting point if you want to write your own plugin. Or you can just use that as a base, and built on top of it.

      Reply
  3. Lorenzo says

    August 9, 2012 at 2:12 am

    hello, is a long time that I’m trying to change this plugin “WP user frontend” to add the Taxonomy form the front end form. My template already integrates Taxonomy so the only thing needed is just to upload data Taxonomy in areas already created from the template.
    In this page http://tareq.wedevs.com/2012/04/how-to-extend-wp-user-frontend
    the author of the plugin explains how to make changes in particular I thought the interesting part would be this:

    :lots of code:

    Can we help me???
    thank you very much!!

    Reply
    • ShibaShake says

      August 9, 2012 at 12:28 pm

      Sorry, I am not familiar with the plugin. Best to contact the plugin author.

      Reply
      • Lorenzo says

        August 11, 2012 at 4:40 am

        I have tried to contact him but it seems to be very valuable in its responses… 🙁

« Previous 1 2

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2025 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·