Add custom fields to a taxonomy

In a project I worked on recently I needed custom fields in some taxonomies. WordPress doesn’t support custom fields for taxonomies, there’s no meta table for taxonomies like there is for posts.

The code below shows a way how it can be done by (ab?)using the options table. For small projects this would be usable, but I wouldn’t recommend it for larger projects with a lot of custom fields.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
// sample on how to add custom fields to your taxonomy

class CustomTaxFields {
    var $taxonomy = 'your_taxonomy';

    function __construct() {
        // Add extra field(s) to the taxonomy
        add_action( $this->taxonomy . '_edit_form_fields', array( $this, 'extra_edit_tax_fields' ), 10, 1 );
        add_action( $this->taxonomy . '_add_form_fields', array( $this, 'extra_add_tax_fields' ), 10, 1 );
        add_action( 'add_' . $this->taxonomy, array( $this, 'save_extra_taxonomy_fields' ), 10, 1 );
        add_action( 'edit_' . $this->taxonomy, array( $this, 'save_extra_taxonomy_fields' ), 10, 1 );
    }

    function extra_edit_tax_fields( $tag ) {
        if ( isset( $tag ) && is_object( $tag ) ) {
            $t_id = $tag->term_id;
            $term_meta = get_option( "taxonomy_$t_id" );
        }
        ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="custom_field"><?php _e('Custom Field'); ?></label></th>
            <td>
                <input type="text" name="term_meta[custom_field]" id="term_meta[custom_field]" value="<?php echo isset($term_meta) && esc_attr($term_meta['custom_field']) ? esc_attr($term_meta['custom_field']) : ''; ?>">
                <p class="description"><?php _e('Custom field added to the taxonomy'); ?></p>
            </td>
        </tr>
        <?php
    }

    function extra_add_tax_fields($tag) {
        if (isset($tag) && is_object($tag)) {
            $t_id = $tag->term_id;
            $term_meta = get_option("taxonomy_$t_id");
        }
        ?>
        <div class="form-field">
            <label for="custom_field"><?php _e('Custom Field'); ?></label>
            <input type="text" name="term_meta[custom_field]" id="term_meta[custom_field]" value="<?php echo isset($term_meta) && esc_attr($term_meta['custom_field']) ? esc_attr($term_meta['custom_field']) : ''; ?>">
            <p class="description"><?php _e('Custom field added to the taxonomy'); ?></p>
        </div>
        <?php
    }

    function save_extra_taxonomy_fields($term_id) {
        if (isset($_POST['term_meta'])) {
            $t_id = $term_id;
            $term_meta = get_option("taxonomy_$t_id");
            $cat_keys = array_keys($_POST['term_meta']);
            foreach ($cat_keys as $key) {
                if (isset($_POST['term_meta'][$key])) {
                    $term_meta[$key] = $_POST['term_meta'][$key];
                }
            }
            update_option("taxonomy_$t_id", $term_meta);
        }
    }
}

Reacties

2 reacties op "Add custom fields to a taxonomy"

  • Safa | 21 February 2013 - 15:52

    Amazing thank you!

  • Wordpress - Aggiungere campi personalizzati alle tassonomie | Sheldon Lab | 26 September 2014 - 11:13

    […] Van Wissel ha pubblicato un nuovo post, in cui spiega con un codice molto semplice come aggiungere un campo personalizzato (e magari più di uno) ad una tassonomia. E’ una semplice classe con pochissimi metodi, e con estrema facilità si possono aggiungere […]

Laat een reactie achter