wpfts_get_sentence_styles (Filter)

The wpfts_get_sentence_styles filter in WP Fast Total Search allows developers to modify the sentence styles used for creating Smart Excerpts. Smart Excerpts are text snippets displayed in search results that contain the found keywords. Sentence styles define how these snippets will be formatted, including adding links, headings, and other elements.

When Used

This filter is useful if you need to change how Smart Excerpts are formatted, for example:

  • Change the format of links to sentences.
  • Add or remove formatting elements, such as headings or captions.
  • Change how keywords are highlighted.
  • Dynamically change styles depending on the context.

Arguments

  • $sentence_styles (array): An array of sentence styles. Each element of the array represents a set of settings for a specific sentence type.

Return Value

  • $sentence_styles (array): The modified array of sentence styles.

$sentence_styles Array Structure

Each element of the $sentence_styles array is an associative array with the following keys:

  • is_on (bool): A flag that enables or disables this style.
  • is_regexp (bool): A flag indicating whether to use a regular expression to match the cluster key.
  • key_term (string): The cluster key or regular expression to match.
  • caption (string): The heading or caption for the sentence. May contain templates such as {{post_url}}, {{sentence}}, {{word}}, {{$1}}, etc.
  • newline_type (int): The newline type (not used in the current plugin version).
  • url_type (int): The link type for the sentence (0 - no links, 1 - heading only, 2 - words only, 3 - heading and words, 4 - sentence only, 5 - heading and sentence).
  • url_template (string): The URL template for the sentence link. May contain templates.
  • class_name (string): The CSS class for the sentence.

Example (Changing the URL Template)

add_filter('wpfts_get_sentence_styles', 'change_sentence_url_template');
 
function change_sentence_url_template($sentence_styles) {
	foreach ($sentence_styles as &$style) { // & - important, modifying the array by reference
		if ($style['key_term'] == 'post_content') {
			$style['url_template'] = '{{post_url}}#wpfts-sentence-{{sentence_id}}'; // adds the sentence ID to the URL
		}
	}
	return $sentence_styles;
}

Important Notes

  • The wpfts_get_sentence_styles filter is called before creating Smart Excerpts.
  • Changes made to the $sentence_styles array affect the formatting of all Smart Excerpts on the page.
  • Make sure you use templates correctly in the caption and url_template fields.
  • Note that the newline_type parameter is currently not used.

The wpfts_get_sentence_styles filter is a powerful tool for customizing the display of Smart Excerpts in WP Fast Total Search, allowing developers to create various formatting options for text snippets in search results.