Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
mgibbs189 / functions.php
Created June 1, 2020 14:18
FacetWP - ignore depth when sorting by display value
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
if ( 'pa_variety' == $facet['name'] ) {
$orderby = 'f.facet_display_value ASC';
}
return $orderby;
}, 10, 2 );
@mgibbs189
mgibbs189 / functions.php
Last active May 29, 2020 17:48
WP Rocket + CDN + FacetWP Template
<?php
// Add to your (child) theme's functions.php
// @link https://docs.wp-rocket.me/article/90-getrocketcdnurl
add_filter( 'facetwp_render_output', function( $output, $params ) {
if ( function_exists( 'get_rocket_cdn_url' ) ) {
$html = $output['template'];
$html = str_replace( get_home_url(), get_rocket_cdn_url( get_home_url() ), $html );
$output['template'] = $html;
@mgibbs189
mgibbs189 / functions.php
Created May 28, 2020 14:26
FacetWP - preserve relevancy if both a search and proximity facet are in use
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_filtered_post_ids', function( $post_ids ) {
$facets = FWP()->facet->facets;
if ( isset( $facets['keywords'] ) && ! empty( $facets['keywords']['selected_values'] ) ) {
remove_filter( 'facetwp_filtered_post_ids', [ FWP()->helper->facet_types['proximity'], 'sort_by_distance' ] );
}
return $post_ids;
@mgibbs189
mgibbs189 / functions.php
Last active May 28, 2020 13:53
FacetWP - Map facet - turn off bounds
<?php
// Add to your (child) theme's functions.php
add_action( 'wp_head', function() {
?>
<script>
(function($) {
$(function() {
if ( 'undefined' !== typeof FWP && 'undefined' !== typeof FWP.hooks) {
@mgibbs189
mgibbs189 / functions.php
Created May 22, 2020 13:05
FacetWP - layout builder - add MP3 URL into an HTML5 audio player
<?php
// Add to your (child) theme's functions.php
// Change "audio-mp3" to your layout builder's item name (which defaults to "el-XXXXX")
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'audio-mp3' == $item['settings']['name'] && ! empty( $value ) ) {
ob_start();
?>
<audio controls>
@mgibbs189
mgibbs189 / functions.php
Created May 21, 2020 13:15
Click "locate me" automatically
<?php
// Add the following to your (child) theme's functions.php
add_action( 'wp_head', function() {
?>
<script>
window.addEventListener('DOMContentLoaded', function() {
if ('undefined' !== typeof FWP.hooks) {
FWP.hooks.addAction('facetwp/loaded', function() {
@mgibbs189
mgibbs189 / functions.php
Created May 11, 2020 22:28
ACF - set "Other data source" equal to "Data source" if empty
<?php
// Add to your (child) theme's functions.php
add_filer( 'facetwp_acf_display_value', function( $value, $params ) {
if ( ! empty( $params['facet_name'] ) ) {
$facet = FWP()->helper->get_facet_by_name( $params['facet_name'] );
if ( ! empty( $facet['source_other'] ) && empty( $value ) ) {
$value = $params['facet_value'];
}
@mgibbs189
mgibbs189 / functions.php
Created May 6, 2020 13:18
User Post Type - change post_name
<?php
// Add to your (child) theme's functions.php
add_filter( 'wp_insert_post_data', function( $data ) {
if ( 'upt_user' == $data['post_type'] ) {
$user_id = (int) $data['post_author'];
$user_data = get_userdata( $user_id );
$slug = $user_data->first_name . '-' . $user_data->last_name . '-' . $user_id;
$data['post_name'] = sanitize_title( $slug );
@mgibbs189
mgibbs189 / functions.php
Created May 1, 2020 19:29
FacetWP - User Post Type - re-index whenever a user is saved
<?php
/**
* This goes within a user save hook
*
* First, we run a UPT sync to copy the data into the wp_post* tables
* Then we re-index in FacetWP to capture the changes
*/
UPT()->sync->run_sync( $user_id );
@mgibbs189
mgibbs189 / functions.php
Created April 28, 2020 18:33
FacetWP - hide the "License key" setting
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_settings_admin', function( $settings ) {
unset( $settings['general']['fields']['license_key'] );
return $settings;
});