Sometimes you want to add more information for taxonomies (category, tags, or any custom taxonomies) in WordPress, like category icon, age (for “people” taxonomy), something like custom fields for posts. But sadly WordPress doesn’t have custom fields for taxonomies. Taxonomy Meta Script For WordPress is a PHP script that helps you easily add meta values to taxonomies, mimic custom post fields for posts in WordPress. It supports various field types.
Table Of Contents
- Features
- Defining boxes
- Showing custom meta fields on website
- For developers: How to extend the class
- Download and enjoy
Features
- Allow to create multiple meta sections
- Support various field types, including: text, textarea, checkbox, checkbox list, radio box, select, wysiwyg, file, image, date, time, color. Developers can easily add more types by extending the script
- Written in OOP, allow developers easily extend the script
Defining meta sections
Defining meta sections for taxonomies is very similar to defining meta boxes for posts. If you have used Meta Box Script for WordPress, you will see this is very easy to understand.
The following code will define 2 meta sections:
$meta_sections = array();
// first meta section
$meta_sections[] = array(
'title' => 'Personal Information', // section title
'taxonomies' => array('people'), // list of taxonomies. Default is array('category', 'post_tag'). Optional
'id' => 'first_section', // ID of each section, will be the option name
'fields' => array( // list of meta fields
array(
'name' => 'Full name', // field name
'desc' => 'Format: Firstname Lastname', // field description, optional
'id' => 'fname', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => 'Anh Tran', // default value, optional
'validate_func' => 'check_name' // validate function, created below, inside RW_Meta_Box_Validate class
)
)
);
// second meta section
$meta_sections[] = array(
'id' => 'additional',
'title' => 'Additional Information',
'taxonomies' => array('category', 'post_tag'),
'fields' => array(
array(
'name' => 'Your thoughts about Deluxe Blog Tips',
'id' => 'thoughts',
'type' => 'wysiwyg', // WYSIWYG editor
'std' => '<b>It's great!</b>',
'desc' => 'Do you think so?'
)
)
);The next variable $meta_sections will hold all information about our meta boxes and all custom fields we need.
Each meta section is an element of $meta_sections array and has the following attributes:
$meta_sections[] = array(
'title' => 'Personal Information', // section title
'taxonomies' => array('people'), // list of taxonomies. Default is array('category', 'post_tag'). Optional
'id' => 'first_section', // ID of each section, will be the option nameid: ID of meta section, each meta section must has an unique ID. This will be used as option name.title: meta section title.taxonomies: array of custom taxonomies. Default isarray('category', 'post_tag'). Optional.
Below are custom fields. Each custom field has the following attributes:
name: name of the custom field.desc: a short description explaining what it is. Optional.id: id of the field, prefixed by$prefix. It will be used to store the custom field value (i.e. meta key)type: field type: text, textarea, checkbox, checkbox list, radio box, select, wysiwyg, file, image, date, time, color- options: is used to declare an array of options for a some type of input (select, radio). Optional.
std: default value of the custom field. Optional.validate_func: validate function, created insideRW_Meta_Box_Validateclass. Optional.format: date or time format, used only for ‘date’ and ‘time’ field typesmultiple: allow field have multiple values. Default isfalse. Optional.style: custom style for field. Optional.
Note that not every field must have all these attributes. Each field type has its own corresponding attributes. Please see details in the taxonomy-box-usage.php in the download package.
Showing taxonomy meta fields on website
Get value of field with single value
Most fields have single value (this is the default option). To get the value of field, use this code:
$meta = get_option('meta_id');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['term_id']) ? $meta['term_id'] : array();
$value = $meta['field_id'];
echo $value; // if you want to showThe meta_id is the ID of meta section that has been registered. The term_id is the ID of term that you want to get meta values. And at last, field_id is the ID of field.
Get value of field with multiple values
There’re some fields that may have multiple values such as select, checkbox_list. To get (or check) values of these fields, use this code:
$meta = get_option('meta_id');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['term_id']) ? $meta['term_id'] : array();
$value = $meta['field_id'];
foreach ($value as $v) {
echo $v;
}Get uploaded files
Uploaded files’ ID are saved as a field with multiple values. Each uploaded image is treated as an attachment. You can use some built-in functions to get attachments’ url or something else.
The following code will show uploaded files.
$meta = get_option('meta_id');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['term_id']) ? $meta['term_id'] : array();
$files = $meta['field_id'];
foreach ($files as $att) {
// show image
echo wp_get_attachment_link($att);
}Get uploaded images
Uploaded images’ ID are saved as a field with multiple values. Each uploaded image is treated as an attachment. This means it has all sizes that defined in Media section or in your theme functions.php file. So, you can use some built-in functions to get image URL, image size, etc.
The following code will show how to get thumbnail url of uploaded images.
$meta = get_option('meta_id');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['term_id']) ? $meta['term_id'] : array();
$images = $meta['field_id'];
foreach ($images as $att) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att, 'full');
$src = $src[0];
// show image
echo "<div style='margin: 0 10px 10px 0; float: left'><img src='$src' /></div>";
}For developers: How to extend the class
Imagine that you want to extend the class that support another field type, for example link. All you need is defining corresponding methods that check, show and save this type of field. That’s very similar to the following:
function __construct($meta_box) {
parent::__construct($meta_box);
$this->check_field_link();
}
// Check field upload and add needed actions
function check_field_link() {
if ($this->has_field('link')) {
// do stuff
}
}
function show_field_link($field, $meta) {}
function save_field_link($post_id, $field, $old, $new) {}1. The name of showing and saving methods should be show_field_{$type} and save_field_{$type}correspondingly.2. The original class has a common save method save_field that can handle most cases of saving field. If you don’t write any method for saving field value (i.e. save_field_{$type} method), then save_field will be automatically used.
Developers also can re-define the look and feel of fields by overwriting the methods of original class.
Download and enjoy
If you like it, please buy me a beer!
License: the Meta Box Script for WordPress is distributed under GNU General Public License.


8 comments
I learned a lot from this post, much appreciated!! :)
Hi I have a slight problem with the uploading image for the taxonomy. I’ve set it up on the clean WP 3.2.1 install and I still can’t upload a picture.
I didn’t change your code at all except for the taxonomy name in order to see the fields in the back but when I try to use the Upload the New Image the screen just goes dark like it was supposed to bring up a new smaller window with details but nothing happens.
Have you ever come across such an issue ?
Let me know if there is something that I need to take into account.
Just a little update. I used the other script as well for the custom fields within the custom post types and it works perfectly as far as it goes for uploading the images
http://www.deluxeblogtips.com/meta-box-script-for-wordpress/
Having verified this I think that the issue is somewhere in the code of the script used for taxonomies, since the same functionality works perfectly in custom post types.
I will try to go through both of them and try to find where it goes off but if you would have an idea – I would be happy to get some heads up.
Other than that your script is really great !!!!
When trying to output uploaded images I am getting an invalid argument in the for loop. My three ID’s seem to be correct. Has anybody else had issues with this?
This is really fantastic – many thanks. Is there any chance of updating it in line with the Meta Box script please?
I’m having the same issues with images.
Cheers,
Barry
I’m focusing on Meta Box Script, and when the problems are fixed, I’ll fix this class, too.
Cool – I’ve put some fixes into the code. Again though, this is some awesome code, very much appreciated. If you would like any help combining the two let me know.
Donation made :o)
Hi Rilwis, is it possible to generate the meta through a loop? I was trying to show the images on a category lists.
Thanks