Gists

7 months ago
<?php
// If you are using an ACF Color Picker field as facet data source,
// you can use the color value (hex or rgb(a)) to give a (background) color to the facet choice.
// It is important to use the facet_display_value for the color, and not the facet_value, 
// because the facet_value will be hashed into a random string of numbers due to unsafe URL characters.
// Note: for fSelect facets, you need to use esc_html() around the label output in L11, otherwise the HTML will be stripped out.
// More info: https://facetwp.com/help-center/using-facetwp-with/advanced-custom-fields/#using-a-color-picker-field

add_filter( 'facetwp_facet_display_value', function( $label, $params ) {
  if ( 'my_color_facet' == $params['facet']['name'] ) { // Replace 'my_color_facet' with the name of your color picker based facet.
    $val = $params['row']['facet_display_value']; // Use the display value here to get the unhashed hex or rgb(a) color value.
    $label = '<span style="background-color:' . $val . ';">' . $label . '</span>'; // Add a span to the facet choice with the color as CSS background color.
  }
  return $label;
}, 20, 2 );
9 months ago
<?php
// A scenario with 2 facets ('locaties' and 'provincie') that both use the same taxonomy 'regio' as data source.
// The provincie facet is set up to index and show only the child terms level of the terms shown in the locaties facet, with the first 'facetwp_index_row' hook snippet
// If a parent term is selected in the locaties facet, the provincie facet will show all child terms in that level, not necessarily direct child terms of the selected parent term in the locaties facet.
// The second snippet makes sure the provincie facet only shows *direct* child terms of the choice selected in the locaties facet.

// Index only the specific depth in the provincie facet.
// Needs re-indexing to work.
add_filter('facetwp_index_row', function($params) {
  if ('provincie' == $params['facet_name']) {
    // Get the ancestors of the term
    $parents = get_ancestors($params['term_id'], 'regio', 'taxonomy');

    // Check if the term is exactly two layers deep
    if (count($parents) !== 2) {
      // If not, set facet_value to an empty string
      $params['facet_value'] = '';
    }
  }
  return $params;
});

// Let the provincie facet only show direct child terms of the choice selected in the locaties facet:
add_filter( 'facetwp_facet_html', function( $output, $params ) {

  // Check if the facet is 'provincie'
  if ('provincie' == $params['facet']['name']) {
    // Get the facet values
    $values = $params['values'];

    // Get the selected value of the 'locaties' facet  
    $selected_value = FWP()->facet->facets['locaties']['selected_values'][0];

    // If the selected value is not null, get the term ID
    if (!is_null($selected_value)) {
      // Get the term by its slug
      $term = get_term_by('slug', $selected_value, 'regio');

      // If the term exists, store the term ID in the global variable
      if ($term) {
        $parent_term_id = $term->term_id;
      }
    }

    // Filter the values based on the parent term
    $filtered_values = array_filter($values, function($value) use ($parent_term_id) {
      // Check if the value's parent is the specified parent term
      return $value['parent_id'] == $parent_term_id;
    });

    // Generate the new facet HTML
    $output = '';
    $selected_values = (array) $params['selected_values'];
    foreach ($filtered_values as $value) {
      $label = esc_html( $value['facet_display_value'] );
      $selected = in_array( $value['facet_value'], $selected_values ) ? ' checked' : '';
      $selected .= ( '' != $value['counter'] && 0 == $value['counter'] && '' == $selected ) ? ' disabled' : '';
      $output .= '<div class="facetwp-checkbox' . $selected . '" data-value="' . esc_attr( $value['facet_value'] ) . '">';
      $output .= '<span class="facetwp-display-value">';
      $output .= apply_filters( 'facetwp_facet_display_value', $label, [
        'selected' => ( '' !== $selected ),
        'facet' => $params['facet'],
        'row' => $value
      ]);
      $output .= '</span>';
      $output .= '<span class="facetwp-counter">(' . $value['counter'] . ')</span>';
      $output .= '</div>';
    }
  }

  return $output;
},10,2);
1 year ago
<?php
/** adds images to the labels in an fselect **/

add_filter( 'facetwp_facet_display_value', function( $label, $params ) {

    if ( 'my_facet_name' == $params['facet']['name'] ) { // Replace "my_facet_name" with the name of your facet
        $term_id = $params["row"]["term_id"]; // get term_id
        $img = // lookup image for the term_id to create an img tag for output
        $img = esc_html( $img ); // esc_html is needed for fselects to have image html in them, otherwise the html is stripped from the display
        $label = $img . $label; // prepends image to label, use $label = $img; if you want to only display image in label
    }

    return $label;    

}, 10, 2 );
2 years ago
<?php

// Add to your (child) theme's functions.php

function wpml_compsupp6751_translate_facet_display_value( $label, $args ) {
    if ( class_exists('Sitepress') ) {
        $wpml_default_lang = apply_filters('wpml_default_language', NULL );
        $wpml_current_lang = apply_filters( 'wpml_current_language', NULL );

        if ($wpml_default_lang == $wpml_current_lang ) {
            do_action( 'wpml_register_single_string', 'FacetWP', 'Facet Display Value : '.substr($label, 0, 10), $label );
        }   
        // Apply the translation to the string
        $label = apply_filters('wpml_translate_single_string', $label , 'FacetWP', 'Facet Display Value : '.substr($label, 0, 10) );
    }
    return $label;
}
add_filter( 'facetwp_facet_display_value', 'wpml_compsupp6751_translate_facet_display_value', 10, 2 );
2 years ago
<?php

/**
 * Manually translate taxonomy terms
 * 
 * Change $facet_name
 * Change $tax_name
 */

add_filter( 'facetwp_facet_display_value', function( $label, $params ) {
    $facet_name = 'my_facet_name';
    $tax_name = 'my_tax_name';

	if ( $facet_name == $params['facet']['name'] ) {
		$current = ( !empty( FWP()->facet->http_params['lang'] ) ) ? FWP()->facet->http_params['lang'] :  apply_filters( 'wpml_current_language', null );  
		$default = apply_filters('wpml_default_language', NULL );
		if ( $current != $default ) {
			$translated_id = apply_filters( 'wpml_object_id', $params['row']['term_id'], $tax_name, TRUE, $current );
			$term = get_term( $translated_id );

			if ( ! empty( $term ) ) {
				$label = esc_html( $term->name );
			}
		}        
	}
	return $label;
}, 10, 2 );
3 years ago
<?php

// Change "categories" to the name of your facet

add_filter( 'facetwp_facet_display_value', function( $label, $params ) {
    if ( 'categories' == $params['facet']['name'] ) {
        $term = get_term_by( 'id', $params['row']['term_id'] );
        if ( ! empty( $term ) && ! empty( $term->description ) ) {
            $label = esc_html( $term->description );
        }
    }
    return $label;
}, 10, 2 );