6 hours ago
<?php
// Fixes closing issues with Avada theme modals that are triggered from inside a listing template:
// Avada moves modals to <body> on page load;
// After a FacetWP refresh (e.g. using facets) the new modal copies stay in the template,
// so the data-target selector now matches two modals, both open, and the extra backdrop can’t be closed.
add_action( 'facetwp_scripts', function() {
?>
<script>
// Before the refresh: remove listing modals that were moved to <body> earlier
document.addEventListener('facetwp-refresh', function() {
if ( ! FWP.loaded ) return; // skip initial page load, Avada handles that
document.querySelectorAll('.facetwp-template [data-toggle="modal"][data-target]').forEach(function(link) {
document.querySelectorAll(link.getAttribute('data-target')).forEach(function(modal) {
if ( ! modal.closest('.facetwp-template') ) {
modal.remove();
}
});
});
});
// After the refresh: move the new listing modals to <body>, like Avada does on page load
document.addEventListener('facetwp-loaded', function() {
document.querySelectorAll('.facetwp-template .fusion-modal').forEach(function(modal) {
document.body.appendChild(modal);
});
});
</script>
<?php
}, 100 );
3 months ago
<?php
/**
** adds a id and label to a select in a facet such as dropdown
** excludes fselects which have a hidden select but display
** as a div element styled to look and behave as a dropdown
**/
add_filter( 'facetwp_facet_html', function( $output, $params ) {
if ( 'fselect' != $params['facet']['type'] && ( empty( $params['facet']['ui_type'] ) || 'fselect' != $params['facet']['ui_type'] ) ) {
$output = str_replace ( '<select ' , '<select id="facet-' . $params['facet']['name'] . '" ' , $output );
$output = str_replace ( '<select' , '<label for="facet-' . $params['facet']['name'] . '">' . $params['facet']['label'] . '</label><select', $output );
}
return $output;
}, 10, 2 );
4 months ago
<?php
/**
** sets tab focus to top of .facetwp-template after a refresh
** .facetwp-template can be changed to whatever element you prefer
**/
add_action('facetwp_scripts', function () {
?>
<script>
(function($) {
document.addEventListener('focus', (e) => {
if ( 'undefined' != typeof e.target.classList && true === e.target.classList.contains( 'facetwp-page' ) ) {
FWP.active_facet = $(e.target.closest('.facetwp-facet')); // emulates setting active_facet for pager when using keyboard enter to select
} else {
FWP.active_facet = null;
}
}, true);
FWP.hooks.addAction('facetwp/loaded', function() {
try {
if ( FWP.loaded && null !== FWP.active_facet && 'pager' == FWP.active_facet.attr('data-type') ) {
let target = document.querySelector('.facetwp-template');
if (!target.hasAttribute('tabindex')) {
target.setAttribute('tabindex', '-1');
}
target.focus();
}
}
catch(e) {
// do nothing
}
}, 101 );
})(fUtil);
</script>
<?php
}, 100);
3 months ago
<?php
// Fix FacetWP query detection issues caused by an 'action' query var added by WP User Front End plugin in /wp-user-frontend/Lib/Gateway/Paypal.php
// Possibly will get fixed in future versions. This bug was noticed in v.4.3.2.
// Bug report: https://github.com/weDevsOfficial/wp-user-frontend/issues/1857
// UPDATE: this issue is FIXED, and this snippet is NOT needed anymore in WP User Front End v4.3.9+
add_filter('query_vars', function ( $vars ) {
if ( function_exists( 'FWP' ) && FWP()->request && FWP()->request->is_refresh ) {
return array_diff( $vars, [ 'action' ] );
}
return $vars;
}, 999, 1);
6 months ago
<?php
// Tells FacetWP to skip the first detected query and use the second.
// Can be useful if for some reason a facetwp-enabled is running twice on a page,
// which will cause a query detection issue and non-functioning facets or pagination.
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
if ( 0 === strpos( FWP()->helper->get_uri(), 'special-project' ) ) {
global $fwp_runonce;
if ( $is_main_query && !isset($fwp_runonce) ) {
$is_main_query = false;
$fwp_runonce = true;
}
}
return $is_main_query;
}, 10, 2 );
8 months ago
<?php
/**
* Example of dynamic tag (https://facetwp.com/help-center/listing-templates/listing-builder/#using-dynamic-tags)
* for FacetWP Listing Builder to display a
* Co-Authors Plus template tag https://github.com/Automattic/Co-Authors-Plus/wiki/Template-tags
* See: https://facetwp.com/help-center/using-facetwp-with/co-authors-plus/#using-a-custom-dynamic-tag-to-display-co-authors
*/
add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
if ( 'co:authors' == $tag_name ) {
$tag_value = coauthors_posts_links( ',', ' and ', null, null, false );
}
return $tag_value;
}, 10, 3 );
8 months ago
<?php
/**
* Set author_name for a FacetWP Listing Builder listing template query on a author archive page
* for compatibility with co-authors plus https://wordpress.org/plugins/co-authors-plus/
* Change 'my_author_listing' in ln 9 to name of the facetwp listing you are using in the author archive
* See: https://facetwp.com/help-center/using-facetwp-with/co-authors-plus/#using-a-listing-builder-listing-in-an-author-archive
*/
add_filter( 'facetwp_query_args', function( $query_args, $class ) {
if ( 'my_author_listing' == $class->ajax_params['template'] ) { // Change 'my_author_listing' to name of the listing you are using in the author archive
$uri_parts = explode( '/', $class->ajax_params['http_params']['uri'] ); // On refresh get author_name from last part of FWP uri
$query_args['author_name'] = ! empty( get_query_var( 'author_name' ) )
? get_query_var( 'author_name' )
: array_pop( $uri_parts );
}
return $query_args;
}, 10, 2 );
8 months ago
<?php
/**
* this is a sample snippet to apply relevanssi click tracking to links
* within the results of a facetwp listing builder template
* on a page with a search facet set to the relevanssi search engine
*/
/**
* hijack facetwp_template_html to modify links within template
* to add relevanssi click tracking
*/
add_filter( 'facetwp_template_html', function( $return, $renderer ) {
if ( 'my_listing' == $renderer->template["name"] ) { // change 'my_listing' to name of your listing
/** there are different filters for get_post type depending on type of posts
* unneeded filters can be removed if they are not included in the search results
**/
add_filter( 'post_link', 'fwp_add_rel_tracking', 10, 2 ); // use for posts
add_filter( 'page_link', 'fwp_add_rel_tracking', 10, 2 ); // use for pages
add_filter( 'post_type_link', 'fwp_add_rel_tracking', 10, 2 ); // use for custom post types
// this fakes a get_search_query string which doesn't get created from a search facet
add_filter( 'get_search_query', function( $keywords ) {
return FWP()->facet->facets["my_search"]["selected_values"]; // change "my_search" to match the name of your search facet
});
}
return $return; // eturn
}, 10, 2);
/**
* hijack facetwp_render output filter to remove relevanssi click tracking
* after facetwp template has completed output
*/
add_filter( 'facetwp_render_output', function( $output, $params ) {
remove_filter( 'post_link', 'fwp_add_rel_tracking', 10 ); // use for posts
remove_filter( 'page_link', 'fwp_add_rel_tracking', 10 ); // use for pages
remove_filter( 'post_type_link', 'fwp_add_rel_tracking', 10 ); // use for custom post types
return $output; // return
}, 10, 2);
/**
* function to hook on get_permalink filters to add relevanssi
* click tracking
*/
function fwp_add_rel_tracking( $link, $post ) {
if ( function_exists( 'relevanssi_add_tracking') ) { // make sure the relevanssi_add_tracking exists
$link = relevanssi_add_tracking( $link );
}
return $link;
}
8 months ago
<?php
// See: https://facetwp.com/help-center/developers/hooks/output-hooks/facetwp_facet_html/#translate-facet-choices
add_filter( 'facetwp_facet_html', function( $output, $params ) {
if ( 'my_facet_name' == $params['facet']['name'] ) { // Replace "my_facet_name" with the name of your facet
// For WPML:
$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 );
// For Polylang, use these 2 lines instead of 2 above
//$current = ( !empty( FWP()->facet->http_params['lang'] ) ) ? FWP()->facet->http_params['lang'] : pll_current_language();
//$default = pll_default_language();
$replace_values = [];
$replace_values['nl'] = [
'Belgium' => 'België',
'Bulgaria' => 'Bulgarije'
];
if ( $current != $default && !empty( $replace_values[$current] ) ) {
$default_values = [];
$new_values = [];
foreach ( $replace_values[$current] AS $replace_value => $replacement ) {
$default_values[] = $replace_value;
$new_values[] = $replacement;
}
$output = str_replace( $default_values, $new_values, $output );
}
}
return $output;
}, 10, 2 );
10 months ago
<?php
/**
** disables new template enahncement buffer in WP 6.9 during facet's ajax refresh
** to disable WP's buffer completely, you can use
** add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_false' );
**/
add_filter( 'wp_should_output_buffer_template_for_enhancement', function( $should ) {
if ( function_exists( 'FWP' ) && FWP()->request && FWP()->request->is_refresh ) {
return false;
}
return $should;
} );