Gists

3 days ago
<?php

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

add_filter( 'term_link', function( $termlink, $term, $taxonomy ) {
    if ( 'category' == $taxonomy ) {
        $termlink = '/good-stuff/?_categories=' . $term->slug;
    }
    return $termlink;
}, 10, 3);
4 days ago
<?php
/** basic filter for pre-selecting a facet choice that corresponds to the current archive term
 ** ex. http://example.com/category/someterm/
 ** only works when term slug is the same as the facet value and
 ** is the last part of the url, in the above example 'someterm' is the term slug
 ** and will be the facet value when selected ?categories=someterm
 **/

add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
    if ( false !== strpos( FWP()->helper->get_uri(), 'category' ) ) { // change 'category' whatever the url is for the term archive
        if ( empty( $url_vars['categories'] ) ) { // change 'categories' to name of the facet here and below
            $term = basename( FWP()->helper->get_uri() );
            $url_vars['categories'] = [ $term ];
        }
    }
    return $url_vars;
} );
3 weeks ago
<?php

add_filter( 'facetwp_index_row', function( $params, $class ) {
    $name = $params['facet_name'];

    // Force-index data from one facet into another
    if ( 'search_by_name' == $name ) {
        $params['facet_name'] = 'category';
    }

    // Append the post type to the facet choice label
    if ( in_array( $name, [ 'search_by_name', 'category' ] ) ) {
        $post_id = (int) $params['post_id'];
        $post_type = get_post_type( $post_id );
        $params['facet_display_value'] .= ' (' . $post_type . ')';
    }

    return $params;
}, 10, 2 );
4 weeks ago
<?php

add_action( 'wp_footer', function() {
?>
<link href="/wp-content/plugins/facetwp/assets/vendor/fSelect/fSelect.css" rel="stylesheet" type="text/css">
<script src="/wp-content/plugins/facetwp/assets/vendor/fSelect/fSelect.js"></script>
<script>
document.addEventListener('facetwp-loaded', function() {
    fUtil('.facetwp-hierarchy_select.is-disabled').addClass('fs-hidden');
    fUtil('.facetwp-hierarchy_select:not(.fs-hidden').fSelect();
});
</script>
<?php
}, 100 );
1 month ago
<script>
/** 
 ** excludes facets from the flyout rather than having
 ** set array of facets to exclude to exclude_flyout
 ** to create an include list manually
 ** in facetwp/flyout/facets filter
 ** https://facetwp.com/help-center/add-on-features-and-extras/mobile-flyout/#customize-the-facets-and-facet-order
 **/

(function($) {
    $(function() {
        if ('object' != typeof FWP) return;
        FWP.hooks.addFilter('facetwp/flyout/facets', function(facets) {	
            const exclude_flyout = [ "search", "another_facet" ];   /* list facets to exclude as array */
            facets.forEach( function( item, index ) {
            if ( exclude_flyout.includes(item) ) {
                facets.splice( index, 1 );
            }
            });
            return facets;
        );
    });
})(jQuery);
</script>
1 month ago
document.addEventListener('facetwp-loaded', function() {
    fUtil('.facetwp-counter').each(function() {
        var count = parseInt(fUtil(this).text().slice(1, -1));
        if (999 < count) {
            var new_count = count.toLocaleString('en-US');
            fUtil(this).text('(' + new_count + ')');
        }
    });
});
1 month ago
<?php
/** creates a dynamic tag for use in the facetwp layout builder - {{ cart }} **/

add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
    if ( 'cart' == $tag_name ) {
        global $product;
        $product = wc_get_product( get_the_ID() );
        ob_start();
        woocommerce_template_loop_add_to_cart();
        $tag_value = ob_get_clean();
    }
    return $tag_value;
  }, 10, 3 );
1 month ago
<?php
/** basic check to keep only one map marker per location
 ** assumes lat is unique without having to check lng also
 **/

Class FWPMapDulicateChecker
{
    public $locations = [];

    function __construct() {

        add_filter( 'facetwp_map_marker_args', function( $args, $post_id ) {
            $lat = $args['position']['lat'];
            if ( false === array_search( $lat, $this->locations ) ) {
                $this->locations[] = $lat;
            } else {
                return false;
            }
            return $args;
        }, 10, 2 );
    }
}
new FWPMapDulicateChecker();
1 month ago
<?php

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

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'resource_center_library_type' == $params['facet_name'] ) {
        $value = $params['facet_value'];
        $value = str_replace( ',', '-', $value );
        $value = str_replace( '&', '-', $value );
        $params['facet_value'] = $value;
    }
    return $params;
}, 10, 2 );
2 months ago
<?php
/**
 ** creates arbitrary values orderby string to sort facets
 ** in order of modifier values in include modifer setting of facet
 ** see https://facetwp.com/help-center/developers/hooks/querying-hooks/facetwp_facet_orderby/#sort-by-arbitrary-values
 **/

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'my_categories' == $facet['name'] ) {
        if ( ! empty( $facet['modifier_values'] ) ) {
            $temp = preg_split( '/\r\n|\r|\n/', trim( $facet['modifier_values'] ) );
            $values = [];
            // Compare using both original and encoded values
            foreach ( $temp as $val ) {
                $val = trim( $val );
                $val_encoded = htmlentities( $val );
                $val_decoded = html_entity_decode( $val );
                $values[ $val ] = true;
                $values[ $val_encoded ] = true;
                $values[ $val_decoded ] = true;
            }
            $values = implode( '","', array_keys( $values ) );            
            $orderby = 'FIELD(f.facet_display_value, "' . $values . '")';
        }
    }
    return $orderby;
}, 10, 2 );