skip to Main Content

I’ve created a custom taxonomy "vendors" in the following way:

// Register Custom Taxonomy

add_action( 'init', 'vendor_taxonomy', 0 );

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',
);

$capabilities = array(
    'manage_terms' => 'manage_woocommerce',
    'edit_terms' => 'manage_woocommerce',
    'delete_terms' => 'manage_woocommerce',
    'assign_terms' => 'manage_woocommerce',
 );

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'show_in_rest'               => true,
    'query_var'                  => true,
    'capabilities'               => $capabilities,


);


register_taxonomy( 'vendors', 'product', $args );
register_taxonomy_for_object_type('vendors', 'product');
}

Inside the taxonomy vendor I created a custom field ‘vendor_email’ in the following way:

<?php

// Add Vendor Email

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

function vendor_email_field_wp_editor_add() {
?>   
<div class="form-field">
    <label for="vendor_email"><?php _e('Email', 'wh'); ?></label>
    <input type="text" name="vendor_email" id="vendor_email">
    <p class="description"><?php _e('Enter Vendor email', 'email'); ?></p>
</div>
<?php
}

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

function vendor_email_field_wp_editor_edit($term) {

$term_id = $term->term_id;

$vendor_email = get_term_meta($term_id, 'vendor_email', true);
?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="vendor_email"><?php _e('Vendor Email', 'email'); ?></label></th>
    <td>
        <input type="text" name="vendor_email" id="vendor_email" value="<?php echo esc_attr($vendor_email) ? esc_attr($vendor_email) : ''; ?>">
        <p class="description"><?php _e('Enter Vendor email', 'email'); ?></p>
    </td>
</tr>
<?php
}

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

function vendor_email_field_wp_editor_save($term_id) {

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

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

The custom field ‘vendor_email’ is registered for REST API usage in the following way:

add_action( 'rest_api_init', 'register_rest_field_email' );

function register_rest_field_email() {
register_rest_field( 'vendors',
    'vendor_email',
    array(
        'get_callback'    => 'email_get_term_meta',
        'update_callback' => 'email_update_term_meta',
        'schema' => null
    )
);}

function email_get_term_meta($object) {
return get_term_meta( $object['id'], 'vendor_email', true );}

function email_update_term_meta( $value, $object, $field_name ) {
if ( ! $value || ! is_string( $value ) ) {
    return;
}
return update_term_meta( $object->ID, $field_name, $value);}

With Wpapi I can correctly retrieve via API call to /wp-json/wp/v2/vendors/ all the data, also the custom field ‘vendor_email’. When I do a post or a put it works correctly on all the standard fields like name or description of the taxonomy "vendors".

But when I try to do a post or a put to create or update the custom fields ‘vendor_email’ it does not work. It seems that it actually cancel the value if already set

I guess the problem is somewhere in the email_update_term_meta() function. But in any case I share here also the function that I created to update "vendor_email" via Wpapi

function updateVendor(vendorId, newVendorEmail){
const WPAPI = require('wpapi');
const wp = new WPAPI({ 
    endpoint: 'http://localhost/fooshi/wp-json',
    username: 'xxxxxxxxx',
    password: 'xxxxxxxxx'
});
wp.vendors = wp.registerRoute( 'wp/v2', '/vendors/(?P<id>)');
return wp.vendors().id(vendorId).update({
    vendor_email: newVendorEmail
}).then(function(res) {
    return res.id;
}).catch((error) => {
    console.log("Response Status:", error);
});

}

2

Answers


  1. Chosen as BEST ANSWER

    Thanks it worked. To detail the solution I removed the register_rest_field_email() block and added:

    add_action( 'rest_api_init', 'register_vendor_email'); 
    
    function register_vendor_email(){
    
    register_term_meta( 'vendors', 'vendor_email', array(
        'type' => 'string',
        'single' => true,
        'show_in_rest' => array(
            'schema' => array(
                'type' => 'string',
                'format' => 'url',
                'context' => array( 'view', 'edit' ),
        )
        ),
    ) );
    }
    

  2. If you’ve set show_in_rest true in the args of a register_taxonomy() call, you do not need any of the code in the first sample. WordPress core provides the get, update, and permissions handler methods for taxonomies if you register them properly. You do not need to use register_rest_field() to update custom taxonomy relationships via the REST API.

    Update:

    Thanks for the additional information. Use register_term_meta() to expose vendor_email in REST.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search