Taxonomy Meta Script For WordPress

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.

Taxonomy Meta 100 out of 100 based on 6 ratings.
Plugin NameTaxonomy Meta
Version1.2
Requires3.3+
Compatible up to3.5.2
Ratings 100/100
Downloaded3068
Last Updated2013-02-21 5:11pm GMT
Contributorsrilwis

Download Stable Version

Download Developer Version

If you like the plugin, please buy be a beer!

Buy me a beer!

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
The plugin is now compatible with WPML plugin – the best multilingual plugin for WordPress

wpml ready badge Taxonomy Meta Script For WordPress

Defining meta sections

Defining meta sections for taxonomies is very similar to defining meta boxes for posts. If you have used Meta Box plugin, you will see this is very easy to understand.

The following code will define 2 meta sections, add it to functions.php file of your theme or your plugin:

Please check the demo.php for full syntax.
$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
        )
    )
);

// 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.
  • 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.

How to get term_id?

To get the term_id of a term, you need to go to edit term page, and look at the URL, like this:

taxonomy meta Taxonomy Meta Script For WordPress

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>";
}