How add body_class from wp_termmeta

I need to assign a class to the body for woocommerce categories.

What I’ve done:

// add the fields when the term is created
add_action( 'product_cat_add_form_fields', 'add_product_cat_class_field', 10, 2 );

function add_product_cat_class_field($taxonomy) {
global $feature_groups;
?><div class="form-field term-group">
    <label for="feature-group"><?php _e('Custom CSS Class', 'my_plugin'); ?></label>
    <input type="text" class="postform" id="custom-class" name="custom-class" value="">
    </select>
</div><?php
}

// save the term meta
add_action( 'created_product_cat', 'save_product_cat_class_meta', 10, 2 );

function save_product_cat_class_meta( $term_id, $tt_id ){
if( isset( $_POST['custom-class'] ) && '' !== $_POST['custom-class'] ){
    $custom_class = sanitize_title( $_POST['custom-class'] );
    add_term_meta( $term_id, 'custom-class', $custom_class, true );
   }
}

// add the fields when the term is being edited
add_action( 'product_cat_edit_form_fields', 'edit_product_cat_class_field', 10, 2 );

function edit_product_cat_class_field( $term, $taxonomy ){

global $feature_groups;

// get current group
$custom_class = get_term_meta( $term->term_id, 'custom-class', true );

?><tr class="form-field term-group-wrap">
    <th scope="row"><label for="feature-group"><?php _e( 'Feature Group', 'my_plugin' ); ?></label></th>
    <td><input type="text" class="postform" id="custom-class" name="custom-class" value="<?php echo esc_attr( $custom_class ); ?>"></td>
</tr><?php
}

add_action( 'edited_product_cat', 'update_product_cat_class_meta', 10, 2 );

 function update_product_cat_class_meta( $term_id, $tt_id ){

if( isset( $_POST['custom-class'] ) && '' !== $_POST['custom-class'] ){
    $custom_class = sanitize_title( $_POST['custom-class'] );
    update_term_meta( $term_id, 'custom-class', $custom_class );
   }
}

I’ts work. I have ‘input’ when creating and editing the category woocommerce.
http://i.stack.imgur.com/Tc1dC.png
Data is stored in the wp_termmeta:
http://i.stack.imgur.com/SLGSk.png

good.

BUT I do not know how now to get it(meta_value) out in body_class

I tried this:

add_filter( 'body_class', 'lalka_custom_taxonomy_in_body_class' );

function lalka_custom_taxonomy_in_body_class( $classes ){

global $feature_groups;

$custom_class = get_the_terms(0, '$product_cat');
// or
//$custom_class = get_term_meta($term_id, 'custom-class', true );
if (!empty($custom_class) ) {
    $classes[] = 'custom-tax-' . $custom_class;
}

  return $classes;
}

Solutions Collecting From Web of "How add body_class from wp_termmeta"

  • First check it is product category archive page using is_tax
  • Then use get queried object_id to get the current term ID
  • Then after you can use get_term_meta to get class name.

Example:

add_filter( 'body_class', 'lalka_custom_taxonomy_in_body_class' );
function lalka_custom_taxonomy_in_body_class( $classes ){
    if (is_tax('product_cat')) {

        $custom_class = !empty( get_queried_object_id() ) ? get_term_meta(get_queried_object_id(), 'custom-class', true) : FALSE;
        if ( !empty($custom_class) ) {
            $classes[] = 'custom-tax-' . $custom_class;
        }

    }
      return $classes;
}