facetwp convert colors

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php
/**
** facetwp’s color addon uses either HTML colors names (HTML color names) or hex values
** the following can map color values that aren’t saved in the correct format for indexing
** remember to do a full re-index in facetwp’s settings after adding your code
** check the wp_facetwp_index table in your database if you need to verify indexing is in the
** correct format
**/
add_filter( ‘facetwp_index_row’, function( $params, $class ) {
if ( ‘colors’ == $params[‘facet_name’] ) { // change ‘colors’ to the name of your facet
/**
** create an array facet_value of your colors to map to hex colors without the #
** for example for product attributes the attribute slug will be the facet_value
**/
$color_map = array(
‘natural-grey’ => ‘e8e8e8’,
‘bright-red’ => ‘FF0000’,
‘sunny-yellow’ => ‘FFFF00’
);
if ( array_key_exists( $params[‘facet_value’], $color_map ) ) {
$params[‘facet_value’] = $color_map[$params[‘facet_value’]];
$params[‘facet_display_value’] = ‘#’ . $params[‘facet_value’];
}
}
return $params;
}, 10, 2 );
{{/gist}}