Gists

5 hours ago
<?php
/** for when some entries are using & and some just & to standardize to keep 
 ** facet from ending up with 2 facet_values that cause the same label to display in 2 choices
 ** generally happens with custom fields not taxonomies
 ** could be used for other encoded characters
 **/
add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( strpos( $params['facet_display_value'], '&' ) > 0 ) {
        $params['facet_display_value'] = str_replace( '&', '&', $params['facet_display_value'] ); // make & standard
        $params['facet_value'] = str_replace( '&', '', $params['facet_display_value'] ); // remove & to prevent md5 hash
    }
    return $params;
}, 10, 2 );
3 weeks ago
<?php
/** 
 * add custom layout builder tag for to retrieve the url of the featured image at a specified size
 * 'medium' can be changed to any default or custom wp image size
 * https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/#image-sizes
 **/
add_filter( 'facetwp_builder_dynamic_tags', function( $tags, $params ) {
    $tags['attachment:url'] = wp_get_attachment_image_url( get_post_thumbnail_id(), 'medium' );
    return $tags;
}, 10, 2 );

/** -----------------------------------------------------
 ** alternative with default image url
 ** ----------------------------------------------------- */

/** 
 * add custom layout builder dynamic tag for to retrieve the url of the featured image at a specified size
 * 'medium' can be changed to any default or custom wp image size
 * https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/#image-sizes
 * change 'http://example.com/image.jpg' to a default image to use when no image url is found
 **/
add_filter( 'facetwp_builder_dynamic_tags', function( $tags, $params ) {
    $tags['attachment:url'] = ( $image = wp_get_attachment_image_url( get_post_thumbnail_id(), 'medium' ) ) ? esc_url( $image ) : 'http://example.com/image.jpg';
    return $tags; 
}, 10, 2 );
1 month ago
<?php

// Hierarchical Checkboxes Facet Accordion with 'opened' class for facet-expand icons for custom CSS styling of opened and closed parents
// Add to your (child)( theme's functions.php

// Adds an 'opened' class to facet-expand of an opened parent.
// Closes other parents on click of a parent
// Also works when the initial page load is with a child selected, causing the parent to be 'opened'.
// The style part is just for demonstration. Use the selectors to style the facetwp-expand icons.

add_action( 'wp_head', function() { ?>

    <style>
        .facetwp-type-checkboxes .facetwp-expand {
            color: green;
        }
        .facetwp-type-checkboxes .facetwp-expand.opened {
            color: red;
        }
    </style>

    <script>
      (function($) {

        // Accordion part 1: toggle 'opened' class on click of icons and close other parents
        $(document).on('click', '.facetwp-type-checkboxes .facetwp-expand', function(e) {
          $(this).toggleClass('opened');
          $(this).closest('.facetwp-checkbox').siblings('.facetwp-checkbox').each(function() {
            $(this).next('.facetwp-depth').removeClass('visible');
            $(this).children('.facetwp-expand').removeClass('opened');
          });
          e.stopPropagation();
        });

        // Accordion part 2: if on first pageload a child is already selected, parent needs a 'opened' class
        $(document).on('facetwp-loaded', function() {
          FWP.hooks.addAction('facetwp/loaded', function() {
            $('.facetwp-type-checkboxes .facetwp-depth').each(function() {
              if ($(this).hasClass('visible')) {
                $(this).prev('.facetwp-checkbox').children('.facetwp-expand').addClass('opened');
              }
            });
          });
        });
      })(jQuery);

    </script>
<?php } );
1 month ago
<?php

// Add to your (child) theme's functions.php
// Disables parent options that have children, in a hierarchical Dropdown facet
// Change 'yourdropdownfacetname' to the name of your dropdown facet
// The code selects parent options with children, which are options that do *not* start with spaces (all children do start with spaces), and adds a 'disabled' attribute to them
// This works only for hierarchies that are one level deep: only top level parents will be disabled.
// It also excludes the 'Any' option

add_action( 'wp_head', function() { ?>

	<script>
        (function($) {
            $(document).on('facetwp-loaded', function() {
                $('.facetwp-facet-yourdropdownfacetname option').filter(function(){

                    var option = $(this).text();
                    var next_option = $(this).next("option").text();
                    if (option.trim() == option && option !== 'Any') { // select parents (do not contain spaces) but not the 'Any' option
                        if (next_option.trim() != next_option) { // if next option is a child (contains spaces)
                            return option;
                        }
                    }

                }).prop("disabled", true);
            });
        })(jQuery);
	</script>
<?php
} );
2 months ago
<?php
/** different hooks needed depending on whether this is a facetwp template or not
 ** in both cases note that the template rather than full $output is modified
 ** potentially for use with image optimization plugins (optimole) that need to modify the
 ** final html in PHP
 **/

/** modifies template for a non-facetwp template **/
add_action( 'facetwp_inject_template', function( $output ) {
    $output['template'] = str_replace( '.jpg', '.svg', $output['template'] );
    FWP()->request->output = $output;
    return; 
}, 10, 2 );

/** modifies template for a facetwp template,
 ** $output needs to be json decoded before modification
 ** and re encoded before returning
 **/
add_filter( 'facetwp_ajax_response', function( $output ) {
    $output = json_decode( $output );
    $output->template = str_replace( '.jpg', '.svg', $output->template );

    // Ignore invalid UTF-8 characters in PHP 7.2+
    if ( version_compare( phpversion(), '7.2', '<' ) ) {
        $output = json_encode( $output );
    }
    else {
        $output = json_encode( $output, JSON_INVALID_UTF8_IGNORE );
    }

    return $output;
}, 10 );
2 months ago
<?php
/** filter main query using while specifiying the page
 ** with FWP()->helper->get_uri()
 ** use page without domain and /'s, ex. 'somepage/mypage'
 **/

add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
    if ( 'news' == FWP()->helper->get_uri() && 'my_cpt' == $query->get( 'post_type' ) ) {
        $is_main_query = false;
    }
    return $is_main_query;

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

// Add to your (child) theme's functions.php
// Applies a class to results based on the facet choice
// Replace 'my_facet_name' with your facet's name (2x)
// Check the class that select each listing result. This depends on your loop setup. In this example it is .fwpl-item
// Change tje desired structure of the class to be applied. In this example it starts with myfacetclass-


add_action( 'wp_head', function () { 

?>
<script>
    document.addEventListener('facetwp-loaded', function() {
    if ('undefined' !== typeof FWP.facets['my_facet_name']) {
        let selected = FWP.facets['my_facet_name'];
        if (selected.length) {
            fUtil('.facetwp-template .fwpl-item').addClass('myfacetclass-' + selected[0]);
        }
    }
 });
    
</script>

<?php 
} );
2 months ago
<?php
/** experimental, use at your own risk, test throughly to not mess up your seo
 ** uses wpseo_robots from wordpress seo plugin (yoast) to change direct links
 ** for facet pages with facets in url from index to noindex in robots meta tag
 **/

add_filter( 'wpseo_robots', function( $robots ) {
    if ( function_exists( 'FWP' ) && ! FWP()->request->is_refresh && !empty( FWP()->request->url_vars )  ) {
        return str_replace( 'index', 'noindex', $robots);
    }
    return $robots;
});
2 months ago
/**
 ** Some metabox fields don't save the right value for display, this can be used to output with metabox function to get correct display value
 ** 'el-99ck4' is name in the item settings in layout builder,
 ** see https://facetwp.com/help-center/developers/hooks/output-hooks/facetwp_builder_item_value/
 ** change 'name_for_meta' to the name of the meta box field, see for more info and additional
 ** functions for getting meta https://docs.metabox.io/displaying-fields/
 **/
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
    if ( 'el-99ck4' == $item['settings']['name'] ) {
        $value = rwmb_meta( 'name_for_meta' );
    }
    return $value;
}, 10, 2 );
3 months ago
<?php

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

add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
    if ( 'food-calendar' == FWP()->helper->get_uri() ) {
        if ( empty( $url_vars['FoodMonth'] ) ) {
            $url_vars['FoodMonth'] = [ date( 'F' ) ];
        }
    }
    return $url_vars;
} );