最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to Add Custom Fields to Custom Taxonomies in Wordpress CLEANLY

programmeradmin5浏览0评论

Saving extra fields that were added to a taxonomy. I want the most proper & efficient way of doing so. Where should I save the new fields?

2 possible solutions are to

1) Use the Wordpress options table as described here... Add Custom Fields to Custom Taxonomies. This is admittedly "not clean" and assumed not to be the correct answer.

// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
    $t_id = $term_id;
    $term_meta = get_option( "taxonomy_term_$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];
        }
    }
    //save the option array
    update_option( "taxonomy_term_$t_id", $term_meta );
}
}

2) Add a new table as described here Custom Taxonomies in Wordpress which follows the naming convention of 'wp_' + customtaxonomykey + 'meta'.

3) Some other option

Saving extra fields that were added to a taxonomy. I want the most proper & efficient way of doing so. Where should I save the new fields?

2 possible solutions are to

1) Use the Wordpress options table as described here... Add Custom Fields to Custom Taxonomies. This is admittedly "not clean" and assumed not to be the correct answer.

// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
    $t_id = $term_id;
    $term_meta = get_option( "taxonomy_term_$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];
        }
    }
    //save the option array
    update_option( "taxonomy_term_$t_id", $term_meta );
}
}

2) Add a new table as described here Custom Taxonomies in Wordpress which follows the naming convention of 'wp_' + customtaxonomykey + 'meta'.

3) Some other option

Share Improve this question asked Jun 27, 2012 at 18:27 csicsi 3772 gold badges5 silver badges16 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 4

Option 2 is the cleanest method - which I've also used a number of times. Unfortunately, there is no default term_metadata table in WordPress yet. This post also covers the same approach, http://shibashake/wordpress-theme/add-term-or-taxonomy-meta-data

And of course, there's a plugin for that too :) http://wordpress/extend/plugins/taxonomy-metadata/

You can save attachment id in options table and get to display that attachment

There are three main functions to add the meta box for taxonomy.Which are invoked through following hooks:

  1. {taxonomy_name}_add_form_fields
  2. {taxonomy_name}_edit_form_fields
  3. edited_{taxonomy_name}
  4. create_{taxonomy_name}

Here you can change the taxonomy_name with any predefined or custom taxonomy accordingly. I am using “Woocommerce product taxonomy” and created a plugin for same. Please review following functions to add custom field:

{taxonomy_name}_add_form_fields add new custom field to add new term page form.Here I am creating a field to add a Class for term.

Add the following code in functions.php in your theme

public function mj_taxonomy_add_custom_meta_field() {
            ?>
            <div class="form-field">
                <label for="term_meta[class_term_meta]"><?php _e( 'Add Class', 'MJ' ); ?></label>
                <input type="text" name="term_meta[class_term_meta]" id="term_meta[class_term_meta]" value="">
                <p class="description"><?php _e( 'Enter a value for this field','MJ' ); ?></p>
            </div>
        <?php
        }
    add_action( 'product_cat_add_form_fields', 'mj_taxonomy_add_custom_meta_field', 10, 2 );

{taxonomy_name}_edit_form_fields add a field on term edit page

public function mj_taxonomy_edit_custom_meta_field($term) {

        $t_id = $term->term_id;
        $term_meta = get_option( "taxonomy_$t_id" ); 
       ?>
        <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[class_term_meta]"><?php _e( 'Add Class', 'MJ' ); ?></label></th>
            <td>
                <input type="text" name="term_meta[class_term_meta]" id="term_meta[class_term_meta]" value="<?php echo esc_attr( $term_meta['class_term_meta'] ) ? esc_attr( $term_meta['class_term_meta'] ) : ''; ?>">
                <p class="description"><?php _e( 'Enter a value for this field','MJ' ); ?></p>
            </td>
        </tr>
    <?php
    }

add_action( 'product_cat_edit_form_fields','mj_taxonomy_edit_custom_meta_field', 10, 2 );

public function mj_save_taxonomy_custom_meta_field( $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];
                }
            }
            // Save the option array.
            update_option( "taxonomy_$t_id", $term_meta );
        }

    }  
add_action( 'edited_product_cat', 'mj_save_taxonomy_custom_meta_field', 10, 2 );  
add_action( 'create_product_cat', 'mj_save_taxonomy_custom_meta_field', 10, 2 );

1) Default wp_options table

I really don't understand why folks propose

update_option( "taxonomy_term_$t_id", $term_meta );

when we can have one single option, whose indexes are the Term ID and the custom fields as values

$options = get_option( 'taxonomy_term_meta' );
$options[$t_id] = $term_meta;
update_option( 'taxonomy_term_meta', $options );

and then simply pull the option and get the value stored for a given Term ID

$options = get_option( 'taxonomy_term_meta' );
echo $options[$tax->term_id];

2) Custom wp_taxonomymeta table

That's what the plugin Taxonomy Metadata, linked by James, does. And it's quite simple, once this table is created, the functions add_, get_, update_ and delete_metadata will start working with 'taxonomy'. Like so:

function add_term_meta($term_id, $meta_key, $meta_value, $unique = false) {
    return add_metadata('taxonomy', $term_id, $meta_key, $meta_value, $unique);
}

function delete_term_meta($term_id, $meta_key, $meta_value = '') {
    return delete_metadata('taxonomy', $term_id, $meta_key, $meta_value);
}

function get_term_meta($term_id, $key, $single = false) {
    return get_metadata('taxonomy', $term_id, $key, $single);
}

function update_term_meta($term_id, $meta_key, $meta_value, $prev_value = '') {
    return update_metadata('taxonomy', $term_id, $meta_key, $meta_value, $prev_value);
}

3) Helper Post Type

As described in Matthew Boynes answer (I think someone mentions this too in the ticket #10142.

4) Outside the Box

In the plugin code, there's a link to the Core Ticket #10142 discussing all this. It's a 4 years old ticket, (closed)(maybelater), lots of developers jumped in, but no conclusion was reached.

By the end of it, we have this nugget (my emphasis):


As I need this regularly, I've wrote my own solution: Misuses the "description" field as storage container for a serialized array. The only thing that was a little tricky was to display the plain description in the admin UI. Anyway this would be the solution I was hoping for: Simply convert the description field instead of adding a bunch of new fields. If there's a need for it, then we will see the use cases after we got it and move for a searchable solution with a later version.

Pretty clever.

The best answer is (3) Some other option.

This is a little unconventional, but it scales the best, leverages core the most, and doesn't require adding a database table: Add a hidden post type for the taxonomy. Each term in the taxonomy gets its own post in the post type. With that in place, the term meta can be stored as post meta. Here's an example of using this approach.

The main issues with any other approach (including the "use the term's description field to hold serialized data" option) are scalability, performance, and compatibility.

https://wordpress/plugins/taxonomy-metadata/

You can customized through above mentioned plugin..

发布评论

评论列表(0)

  1. 暂无评论