Gists

3 days ago
<?php
// FacetWP does NOT officially support TranslatePress. It supports WPML and Polylang, with the Multilingual add-on:
// https://facetwp.com/help-center/using-facetwp-with/multilingual/

// Some users do use TranslatePress successfully with FacetWP.
// However, we get reports of inconsistent translations of facet choices before and after facet filtering.
// These issues are reportedly solved by adding this refresh snippet to the (child) theme's functions.php:

add_action('facetwp_scripts', function () {
  ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      if ( !FWP.loaded ) {
        FWP.loaded = true;
        FWP.refresh();
      }
    });
  </script>
  <?php
}, 100);
4 months ago
<?php
add_action('facetwp_scripts', function () {
  ?>
  <script>
    (function($) {
      document.addEventListener('facetwp-refresh', function() {
        if ( 'undefined' != typeof FWP_MAP && true === FWP_MAP.is_filtering) {
          $('.facetwp-location').val(''); // reset the value of the input field
          $('.facetwp-lat').val(''); // reset hidden field
          $('.facetwp-lng').val(''); // reset hidden field
          $('.facetwp-radius option').each(function () {
            if (this.defaultSelected) {
              this.selected = true;
              return false;
            } // reset default radius if it is a dropdown
          });
          FWP.facets['my_proximity_facet'] = []; // change 'my_proximity_facet' to name of your Proximity facet
        }
      });
    })(fUtil);
  </script>
  <?php
}, 100);
6 months ago
<?php
// Resets a facet on first page load only, if it has a selection in the URL. 
// Can be used if users have bookmarked links with now non-existing facet choices.
// Retains other facet choices.

// Part 1: Reset the facet choice by preloading the empty url vars for it on first page load
add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
  if ( 'demo/cars' == FWP()->helper->get_uri() ) { // Replace 'demo/cars' with the URI of your page (everything after the domain name, excluding any slashes at the beginning and end)
    if ( ! empty( $url_vars['myfacetname'] ) ) { // Replace 'myfacetname' with the name of your facet
      $url_vars['myfacetname'] = []; // Replace 'myfacetname' with the name of your facet
    }
  }
  return $url_vars;
} );

// Part 2: Remove the facet's URL vars for the facet
add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      if ( ! FWP.loaded ) { // Only on first page load
        var facet_name = 'myfacetname';
        if ( 'object' == typeof FWP.facets[facet_name] && FWP.facets[facet_name].length  ) {
          FWP.facets[facet_name] = []; // Remove choices
          FWP.setHash(); // Reset the URL vars
        }
      }
    });
  </script>
  <?php
}, 100 );
1 month ago
<?php

// https://facetwp.com/help-center/developers/javascript-reference/facetwp-refresh/#reset-all-other-facets-on-change-of-a-specific-facet

add_action( 'facetwp_scripts', function() { ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      let facet_name = 'my_facet_name'; // Change 'my_facet_name' to the name of the facet that resets other facets.
      if ( null !== FWP.active_facet && facet_name == fUtil(FWP.active_facet.nodes[0]).attr('data-name' ) ) {
        let others = FWP.facets;
        Object.keys(others).forEach(function (key) {
          if ( facet_name != key ) {
            FWP.facets[key] = [];
          }
        });
      }
    });
  </script>
<?php }, 100 );
9 months ago
<?php 
// Pre-select the first facet choice when this choice is unknown / different on each page the facet is placed on
// To pre-select a know choice, use the 'facetwp_preload_url_vars' hook instead:
// https://facetwp.com/help-center/developers/hooks/querying-hooks/facetwp_preload_url_vars/

add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      if ( !FWP.loaded ) { // Only on first page load
        var facet_name = 'my_facet_name';  // Change 'my_facet_name' to the name of your facet
        if ( 'object' == typeof FWP.facets[facet_name] && 1 > FWP.facets[facet_name].length ) {
          var temp = document.createElement('div');
          temp.innerHTML = FWP_JSON.preload_data.facets[facet_name];
          var first = temp.getElementsByClassName('facetwp-radio'); // for a Radio facet, change if needed
          first = first[1]; // Pre-select the second choice, skips the first "Any" choice. Use first[0] if there is no "Any" choice
          FWP.facets[facet_name] = [ first.getAttribute('data-value') ];
          FWP.loaded = 1; // force TRUE to make an AJAX request & populate results
        }
      }
    });
  </script>
  <?php
}, 100 );


// Alternative: Pre-select using a page refresh with window.location.href instead of facet refresh

add_action( 'facetwp_scripts', function() {
    ?>
      <script>
        document.addEventListener('facetwp-refresh', function() {
            if ( !FWP.loaded ) {
                var facet_name = 'my_facet_name';  // Change 'my_facet_name' to the name of your facet
                if ( 'object' == typeof FWP.facets[facet_name] && 1 > FWP.facets[facet_name].length ) {
                    var temp = document.createElement('div');
                    temp.innerHTML = FWP_JSON.preload_data.facets[facet_name];
                    var first = temp.getElementsByClassName('facetwp-radio'); // for a Radio facet, change if needed
                    first = first[1]; // Pre-select the second choice, skips the first "Any" choice. Use first[0] if there is no "Any" choice
                    FWP.facets[facet_name] = [ first.getAttribute('data-value') ];
                    let qs = FWP.buildQueryString();
                    let href = window.location.href
                    let prefix = (-1 < href.indexOf('?')) ? '&' : '?';
                    window.location.href = href + prefix + qs;
                }
            }
        });
      </script>
    <?php
}, 100 );
10 months ago
<?php
/** this will migrate old urls with sort value to new sort facet value
 ** if you have a facetwp_sort_options filter, you need to keep it
 ** sort option name/slug (value displayed in url) in the new sort facet needs to 
 ** match old sort
 **/

add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
    if ( ! empty( $url_vars['sort'] ) ) { 
        $url_vars['my_sort_facet'] = $url_vars['sort']; // change 'my_sort_facet' to name of your sort facet
    }
    return $url_vars;
});

add_action( 'facetwp_scripts', function() {
    ?>
    <script>
        document.addEventListener('facetwp-refresh', function() {
            if ( FWP.loaded ) {
                FWP.extras.sort = '';
            }
        });
    </script>
    <?php
}, 100 );
10 months ago
<?php

add_action( 'facetwp_scripts', function() { ?>
    <script>
        (function($) {
            document.addEventListener('facetwp-refresh', function() {
                if ( FWP.soft_refresh == true )  {
                    FWP.enable_scroll = true;
                } else {
                    FWP.enable_scroll = false;
                }
            });
            document.addEventListener('facetwp-loaded', function() {
                if (FWP.enable_scroll == true) {
                    var template = document.getElementsByClassName('facetwp-template');
                    if ( 'object' == typeof template && undefined != template[0] ) {
                        var offset = template[0].offsetTop;
                        window.scrollTo({
                            top: offset,
                            behavior: "smooth",
                        });
                    }
                }
            });
        })(fUtil);
    </script>
<?php } );
11 months ago
<?php
add_action( 'facetwp_scripts', function() {
    ?>
    <style>
        .facetwp-template {
            opacity: 1;
            transition: opacity 0.5s linear;
        }
        .facetwp-template.template-loading {
            opacity: 0;
        }
    </style>
    <script>
        document.addEventListener('facetwp-refresh', function() {
            if ( FWP.loaded ) {
                fUtil( '.facetwp-template' ).addClass( 'template-loading' );
            }
        });
        FWP.hooks.addAction('facetwp/loaded', function() { 
            fUtil( '.facetwp-template' ).removeClass( 'template-loading' );
        });
    </script>
    <?php
}, 100 );
12 months ago
<?php
add_action( 'facetwp_scripts', function() {
    ?>
      <script>
        document.addEventListener('facetwp-refresh', function() {
            if ( !FWP.loaded ) {
                FWP.setHash();
                FWP.fetchData();
            }
        });
      </script>
    <?php
  }, 100 );
1 year ago
<?php
/** resets a facet changing another facet
 ** change 'facet_name_1' and 'facet_name_2' to the names of your facets
 **/
add_action( 'wp_head', function() { ?>
    <script>
    document.addEventListener('facetwp-refresh', function() {
        if ( null !== FWP.active_facet && 'facet_name_1' == fUtil(FWP.active_facet.nodes[0]).attr('data-name' ) ) {
            FWP.facets['facet_name_2'] = [];
        }
    });
    </script>
<?php }, 100 );