wpfts_apply_sentence_styles (Filter)
The wpfts_apply_sentence_styles
filter in WP Fast Total Search allows developers to modify the HTML code of individual sentences in Smart Excerpts before they are combined into the final excerpt. This filter provides the ability to apply custom styles, add links, or make other changes to each sentence found in the post text and matching the search query.
When to Use
This filter can be useful in the following cases:
- Applying custom styles to sentences: You can add CSS classes or inline styles to each sentence.
- Adding links to sentences: You can wrap each sentence in a link leading to the post page with the highlighted sentence.
- Modifying sentence text: You can change the sentence text before output, for example, shorten it or add a prefix/suffix.
Arguments
$outtext
(string): The HTML code of the sentence generated by the plugin.$sentence_styles
(array): An array of sentence styles defined in the plugin settings.$key
(string): The key of the cluster from which the sentence was extracted.$words
(array): An array of search words.$sentence
(string): The original text of the sentence.
Return Value
$outtext
(string): The modified HTML code of the sentence.
Example (Adding a CSS class to each sentence)
add_filter('wpfts_apply_sentence_styles', 'add_custom_class_to_sentences', 10, 5);
function add_custom_class_to_sentences($outtext, $sentence_styles, $key, $words, $sentence) {
// adds the 'my-custom-sentence-class' class to each sentence
$outtext = str_replace('<span class="wpfts-sentence">', '<span class="wpfts-sentence my-custom-sentence-class">', $outtext);
return $outtext;
}
Important Notes
- The
wpfts_apply_sentence_styles
filter is called for each sentence in Smart Excerpts. - Changes made to
$outtext
will be displayed on the search results page. - You can use the arguments
$sentence_styles
,$key
,$words
, and$sentence
for dynamically changing the HTML code of the sentence.
The wpfts_apply_sentence_styles
filter provides developers with the ability to fine-tune the display of Smart Excerpts in WP Fast Total Search.