How to add a custom taxonomy field in woocommerce product category or any other custom taxonomy in wordpress

For add a custom taxonomy  field in product category or any other custom taxonomy
Add below code in functions.php file of your theme.

function product_cat_add_video_field_rj() {
   
    ?>
    <div class="form-field">
        <label for="term_meta[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label>
        <input type="text" name="term_meta[video_link]" id="term_meta[video_link]" value="">
        <p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
    </div>
<?php
}
function product_cat_edit_video_field_rj($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[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label></th>
        <td>
            <input type="text" name="term_meta[video_link]" id="term_meta[video_link]" value="<?php echo esc_attr( $term_meta['video_link'] ) ? esc_attr( $term_meta['video_link'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
        </td>
    </tr>
<?php
}
// for any other custom taxonomy action like {custom_taxonomy}_add_form_fields  here i used product_cat as {custom_taxonomy }
// for any other custom taxonomy action like {custom_taxonomy}_edit_form_fields here i used product_cat as {custom_taxonomy }

// this action use for add field in add form of taxonomy
add_action( 'product_cat_add_form_fields', 'product_cat_add_video_field_rj', 10, 2 );
// this action use for add field in edit form of taxonomy
add_action( 'product_cat_edit_form_fields', 'product_cat_edit_video_field_rj', 10, 2 );
function product_cat_video_link_save( $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 );
    }
}
// for any other custom taxonomy action like edited_{custom_taxonomy }  here i used product_cat as {custom_taxonomy }
// for any other custom taxonomy action like create_{custom_taxonomy }  here i used product_cat as {custom_taxonomy }

// this action use for save field value of edit form of taxonomy
add_action( 'edited_product_cat', 'product_cat_video_link_save', 10, 2 ); 
// this action use for save field value of add form of taxonomy
add_action( 'create_product_cat', 'product_cat_video_link_save', 10, 2 );

Comments

  1. How do I retrieve the option to display on front end?

    ReplyDelete
    Replies
    1. As per above example.
      You can get option by using term id

      $t_id = $term->term_id;
      $term_meta = get_option( "taxonomy_$t_id" );

      If you have any other question feel free to ask.

      Delete
    2. Thanks. I used

      global $post;

      //for product cat archive page only
      $term = get_queried_object();
      $metaArray = get_option('taxonomy_' . $term->term_id);
      $productCatMetaTitle = $metaArray['video_link'];

      ?>

      Delete

Post a Comment

Popular posts from this blog

How to add image option in nav menu in wordpress

How to add a custom email functionality in woocommerce,wordpress for example new email template for shipping order

How to change user id on checkout page for assign order to different user in woocommerce wordpress