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 –
- meta_id – Contains the unique id of each piece of meta-data information that is stored in our taxonomy meta-data table.
- {$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.
- meta_key – Meta-data key.
- 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.
Al says
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
Karen says
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!)
🙂
ShibaShake says
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.
Lorenzo says
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!!
ShibaShake says
Sorry, I am not familiar with the plugin. Best to contact the plugin author.
Lorenzo says
I have tried to contact him but it seems to be very valuable in its responses… 🙁