Skip to content

Instantly share code, notes, and snippets.

@djrmom
Last active February 22, 2018 01:16
Show Gist options
  • Select an option

  • Save djrmom/452aea7e05432a536212b009213d313e to your computer and use it in GitHub Desktop.

Select an option

Save djrmom/452aea7e05432a536212b009213d313e to your computer and use it in GitHub Desktop.
facetwp convert colors
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment