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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // 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
1 2 | 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.
1 2 3 4 | // 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 –
1 | $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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?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.