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

  1. Features
  2. Defining boxes
  3. Showing custom meta fields on website
  4. For developers: How to extend the class
  5. 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 name
  • id: 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 is array('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 inside RW_Meta_Box_Validate class. Optional.
  • format: date or time format, used only for ‘date’ and ‘time’ field types
  • multiple: allow field have multiple values. Default is false. 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 show

The 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

Download version 1.0

If you like it, please buy me a beer!

Donation

License: the Meta Box Script for WordPress is distributed under GNU General Public License.