wpfts_preset_data (Filter)

The wpfts_preset_data filter in WP Fast Total Search allows developers to modify preset data before it’s used. Presets in WPFTS define search settings such as the results page URL, autocomplete mode, used clusters and their weights, and other parameters. This filter provides the ability to dynamically change these settings depending on the context in which the search is called.

When Used

This filter can be useful in the following cases:

  • Changing the results page URL depending on the current page: For example, you can configure the preset so that search results are displayed on different pages for different sections of the site.
  • Enabling/disabling autocomplete depending on the device: You can disable autocomplete on mobile devices to improve performance.
  • Changing cluster weights depending on the context: For example, on a product category page, you can increase the weight of the cluster containing the product description.
  • Adding additional parameters to the preset: You can add your own parameters to the preset and use them in your code.

Arguments

  • $preset (array): An associative array of preset data. Contains keys such as results_url, autocomplete_mode, classname, and others.
  • $preset_id (string): The preset identifier.
  • $context (string): The context in which the preset is used. Currently, the value ‘widget’ is used.

Return Value

  • $preset (array): The modified array of preset data.

Example (Changing the Results Page URL)

add_filter('wpfts_preset_data', 'my_wpfts_preset_data_filter', 10, 3);
function my_wpfts_preset_data_filter($preset, $preset_id, $context) {
  if ($preset_id == 'my_custom_preset') {
    if (is_singular('product')) {
        // If we are on a product page, configure the search for the product category
		$terms = get_the_terms(get_the_ID(), 'product_cat');
		if ($terms && !is_wp_error($terms)) {
			$term = array_pop($terms);
			$preset['results_url'] = get_term_link($term);
		}
    } else {
      $preset['results_url'] = home_url('/search/'); // Default results page URL.
    }
  }
  return $preset;
}

Important Notes

  • The wpfts_preset_data filter is called before the preset is used.
  • Changes made to the $preset array affect the search settings.
  • The $context argument is currently always ‘widget’, but other contexts may be added in future plugin versions.

The wpfts_preset_data filter provides developers with a powerful tool for dynamically configuring search presets in WP Fast Total Search.