patric-boehner
7/16/2016 - 12:25 AM

Add Color Term Meta Selection To Custom Taxonomy & Output

Add Color Term Meta Selection To Custom Taxonomy & Output

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

//**********************************************
// Testimonials Taxonomy Metabox
//**********************************************


add_action( 'cmb2_admin_init', 'pb_cmb2_testimonials_taxonomy_meta' );
/**
* Hook in and add a metabox to add fields to taxonomy terms
*/
function pb_cmb2_testimonials_taxonomy_meta() {
	$prefix = '_pb_testimonial_color_';
	/**
	* Metabox to add fields to categories and tags
	*/
	$cmb_testimonial_color = new_cmb2_box( array(
		'id'               => 'testimonial-term-meta',
		'title'            => __( 'Testimonial Category Color Metabox', 'cmb2' ), // Doesn't output for term boxes
		'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
		'taxonomies'       => array( 'testimonial-category' ), // Add to custom taxonomy
		'new_term_section' => true, // Will display in the "Add New Category" section
		) );
		$cmb_testimonial_color->add_field( array(
			'name'             => 'Category Color',
			'desc'             => 'Select a color to assign to this testimonial category. The color is used to distinguish testimonials and is show when no image is set the for testimonial.',
			'show_names'		  => true,
			'id'               => $prefix . 'color_select',
			'type'             => 'select',
			'show_option_none' => false,
			'default'          => '892f6b',
			'options'          => array(
				'892f6b'	=> __( 'Color One', 'cmb2' ),
				'651b72'	=> __( 'Color Two', 'cmb2' ),
				'd9535b'	=> __( 'Color Three', 'cmb2' ),
				'a64f80'	=> __( 'Color Four', 'cmb2' ),
			),
		) );
}
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

//**********************************************
// Testimonials Taxonomy Admin Columns
//**********************************************
// https://www.smashingmagazine.com/2015/12/how-to-use-term-meta-data-in-wordpress/
// https://wordpress.stackexchange.com/questions/77658/custom-columns-for-taxonomy-list-table

// Add to admin_init function
add_filter("manage_edit-testimonial-category_columns", 'pb_testimonial_tax_columns');

function pb_testimonial_tax_columns( $columns ) {

   $columns = array(
     'cb'      => '<input type="checkbox" />',
     'name'    => __('Name'),
     'header_icon' => '',
     'description' => __('Description'),
     'slug'    => __('Slug'),
     'posts'   => __('Posts'),
     'color'   =>__('Color'),
     );

   return $columns;

}


// Add to admin_init function
add_filter("manage_testimonial-category_custom_column", 'pb_add_tax_column_content', 10, 3);
function pb_add_tax_column_content( $content, $column_name, $term_id ){

   global $terms;
   switch ( $column_name ) {

      //* If displaying the 'color' column.
      case 'color':

      //* Get the color meta
      // $term_id = absint( $term_id );
      $color = get_term_meta( $term_id, '_pb_testimonial_color_color_select', true );
      $markup = '<i class="term-color" style="background-color:#' . esc_attr( $color ) . '"></i>';

      if ( !empty( $color )) {
         $content = $markup;
      } else {
      	$content = 'N/A';
      }
      break;

      //* Just break out of the switch statement for everything else.
      default:
      break;

   }

   return $content;

}


// Style the color admin column
add_action( 'admin_head', 'my_custom_admin_head' );
function my_custom_admin_head() {

   if ( function_exists('pb_testimonial_tax_columns') && is_admin() && is_blog_admin() ) {

      echo '<style type="text/css">
      .column-color {
         width: 50px;
      }
      .term-color {
         height: 25px;
         width: 25px;
         display: inline-block;
         border: 2px solid #eee;
         border-radius: 100%;
      }
      </style>';

   }
}
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

/*
* An example outputing the term meta. In this case, a color used as a fallback background color
* when a background image isn't available (the img variable isn't represented in this example).
*/

// cmb2 prefix
$tax_prefix = '_pb_testimonial_color_';

// Get the taxonomy metadata for the color
$terms = get_the_terms( get_the_ID(), 'testimonial-category');
$color = esc_attr( get_term_meta( $terms[0]->term_id, '' .$tax_prefix. 'color_select', true ) );

// Conditional output of inline style
if ( empty( $color ) ) {
	$bk_color = '';
} else {
	$bk_color = 'style="background-color:#' .$color. '"';
}

//**********************

// Output an image else output the taxonomy meta color as background color.

//* Conditionaly add clients image
if ( !empty( $image ) ) {
	echo '<div class="testimonial-image first one-fourth">' .$image. '</div>';
} else {
	echo '<div class="testimonial-image first one-fourth" ' .$bk_color. '></div>';
}