Extending Functionality

5. Extending Functionality

WP Fast Total Search (WPFTS) provides a powerful search engine, but its standard functionality may be insufficient for some projects. Developers may need to extend WPFTS functionality to address specific tasks, such as:

  • Indexing Additional Data: WPFTS defaults to indexing post titles, content, and excerpts. You may need to index additional data, such as meta fields, taxonomy terms, data from other plugins, or even data from external sources.

  • Modifying the Search Algorithm: You can change how WPFTS processes search queries, for example, add synonym support or change cluster weights for more accurate ranking of results.

  • Modifying Result Display: You can customize the appearance of search results, add or remove information, change the display format of Smart Excerpts, etc.

  • Integration with Other Plugins: You can integrate WPFTS with other plugins to extend search capabilities or add new features.

Typical Examples of Functionality Extension

  • Indexing WooCommerce Data: Adding product data to the index, such as price, SKU, attributes, etc.

  • Indexing Custom Meta Fields: Adding the values of custom meta fields associated with posts to the index.

  • Changing Cluster Weights for Different Post Types: Increasing the title weight for news and decreasing the content weight for pages.

  • Adding Category Filtering to the Search Widget: Allows users to restrict searches to specific categories.

Main Ways to Extend Functionality

WPFTS provides two main ways to extend functionality:

  1. Using Hooks (actions and filters) in your theme’s or child theme’s functions.php file: This is the easiest way for minor changes. You can add your callback functions to WPFTS hooks to modify its behavior. A detailed description of all available hooks is provided in the relevant sections of this documentation.

  2. Writing an Addon Plugin: For more complex extensions, it is recommended to create a separate addon plugin. This will allow for better code organization and avoid conflicts with other plugins. The addon can contain its own settings, scripts, and styles. It can interact with WPFTS through hooks and filters, as well as through the plugin’s API.

Example of Extending Functionality via functions.php (Adding a Meta Field to the Index)

add_filter('wpfts_index_post', 'add_custom_meta_to_index', 10, 3);
 
function add_custom_meta_to_index($chunks, $post, $is_refresh_raw_cache) {
    $custom_field_value = get_post_meta($post->ID, 'my_custom_field', true);
	if ($custom_field_value) {
		$chunks['my_custom_field'] = $custom_field_value;
	}
 
	return $chunks;
}

Example Addon Plugin Structure

wpfts-addon-customdata/
├── wpfts-addon-customdata.php    // Main plugin file
└── includes/
    └── addon-functions.php // File with addon functions

It is strongly recommended to follow the naming convention wpfts-addon-<addon name> for addons. This will help group addons in the plugins list.

In the wpfts-addon-customdata.php file, you can register your addon and include the necessary files. In the addon-functions.php file, you can place the code that will interact with WPFTS through hooks and filters.

Detailed examples of extending WPFTS functionality are provided in the “Usage Examples” section.