简体   繁体   中英

Adding Custom Fields to Woocommerce Custom Taxonomy

I've created a custom taxonomy called "vendors" in Woocommerce through this:

function vendor_taxonomy()  {
$labels = array(
    'name'                       => 'Vendors',
    'singular_name'              => 'Vendor',
    'menu_name'                  => 'Vendors',
    'all_items'                  => 'All Vendors',
    'parent_item'                => 'Parent Vendor',
    'parent_item_colon'          => 'Parent Vendor:',
    'new_item_name'              => 'New Vendor Name',
    'add_new_item'               => 'Add New Vendor',
    'edit_item'                  => 'Edit Vendor',
    'update_item'                => 'Update Vendor',
    'separate_items_with_commas' => 'Separate Vendors with commas',
    'search_items'               => 'Search Vendors',
    'add_or_remove_items'        => 'Add or remove Vendors',
    'choose_from_most_used'      => 'Choose from the most used Vendors',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'item', 'product', $args );
}
add_action( 'init', 'vendor_taxonomy', 0 );

Now I would like to customize the fields in the admin page. Default fields are Name, Slug and Description. Let's say I would like to add an extra text fields called "featured products", I'm trying something like this below, but nothing happens in the admin page of the taxonomy:

add_action('vendors_add_form_fields', 'vendors_featured_wp_editor_add', 10, 1);

function vendors_featured_wp_editor_add() {
?>    
<div class="form-field">
    <label for="featured_products"><?php _e('Featured Products', 'wh'); ?></label>
    <input type="text" name="featured_products" id="featured_products">
    <p class="description"><?php _e('Enter 4 featured products, Comma separated', 
    'featured'); ?></p>
</div>
<?php
}

...

add_action('vendors_edit_form_fields', 'vendors_featured_wp_editor_edit', 10, 1);

function vendors_featured_wp_editor_edit($term) {

$term_id = $term->term_id;

$featured_products = get_term_meta($term_id, 'featured_products', true);
?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="featured_products"><?php _e('Featured 
    Products', 'featured'); ?></label></th>
    <td>
        <input type="text" name="featured_products" id="featured_products" value="<? 
        php echo esc_attr($featured_products) ? esc_attr($featured_products) : ''; ? 
        >">
        <p class="description"><?php _e('Enter 4 featured products, Comma separated', 
        'featured'); ?></p>
    </td>
</tr>
<?php
}

...

add_action('edited_vendors', 'vendors_featured_save_wp_editor', 10, 1);
add_action('create_vendors', 'vendors_featured_save_wp_editor', 10, 1);

function vendors_featured_save_wp_editor($term_id) {

$featured_products = filter_input(INPUT_POST, 'featured_products');

update_term_meta($term_id, 'featured_products', $featured_products);
}

You registered taxonomy with taxonomy key item :

register_taxonomy( 'item', 'product', $args );

Then you try to add hook to vendors taxonomy key:

function vendors_featured_wp_editor_edit($term) {

Check the name of your taxonomy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM