wpfts_split_to_words (Filter)
The wpfts_split_to_words
filter in WP Fast Total Search allows developers to modify the word splitting algorithm used by the plugin during content indexing. This enables adaptation of the plugin to various languages or specific text processing requirements.
When to Use
This filter can be useful if:
- The standard word splitting algorithm does not work correctly with your language or content.
- You need to process specific characters or character sequences in a special way.
- You want to exclude certain words or characters from the index.
Arguments
$words
(array): An array of words obtained after splitting the text using the standard algorithm.$text
(string): The original text that was split into words.
Return Value
$words
(array): The modified array of words.
Example
/**
* Modifies the text splitting algorithm using a regular expression to support Cyrillic characters.
*/
add_filter('wpfts_split_to_words', 'my_wpfts_split_to_words_filter', 10, 2);
function my_wpfts_split_to_words_filter($words, $text) {
// Use a regular expression to split the text into words,
// supporting Cyrillic and Latin characters.
preg_match_all('/[\p{L}\p{N}\']+/u', $text, $matches);
return isset($matches[0]) ? $matches[0] : array();
}
Example with Removing Specific Words
add_filter('wpfts_split_to_words', 'remove_specific_words', 10, 2);
function remove_specific_words($words, $text)
{
$words_to_remove = array('and', 'or', 'the'); // List of words to remove
$filtered_words = array();
foreach ($words as $word) {
if ( !in_array(strtolower($word), $words_to_remove) ) {
$filtered_words[] = $word;
}
}
return $filtered_words;
}
Important Notes
- The
wpfts_split_to_words
filter is called during content indexing, not during search. - Carefully test your custom text splitting algorithm to ensure that it works correctly with your content.
- Changes made to the word splitting algorithm affect the index content. After changing the algorithm, you may need to rebuild the index.
The wpfts_split_to_words
filter provides developers with flexibility in customizing the content indexing process in WP Fast Total Search.