Learn How to extend default search logic for baltic special characters
In this article, we will cover how to fix search functionality for Baltic words that contain special characters like ā, ē, ī, and others.
Problem:
By default, the Advanced Woo Search plugin converts such characters to their Latin equivalents. For example:
- ā → a
- ē → e
- ī → i
This works correctly most of the time but can lead to issues in specific cases.
Example:
In Latvian, the word saka translates to "to say," while sākas means "to begin."
By default, searching for sāka will display products containing both words, sākas and saka.
Goal:
We want the search to return only products containing the word sākas.
This issue can be resolved by using a custom code snippet to replace special characters with unique representations, different from the default Latin equivalents.
Code Snippet:
add_filter( 'aws_diacritic_chars', 'my_aws_diacritic_chars' ); function my_aws_diacritic_chars( $chars ) { $chars['ā'] = 'a_'; $chars['ē'] = 'e_'; $chars['ī'] = 'i_'; $chars['ū'] = 'u_'; $chars['č'] = 'c_'; $chars['ģ'] = 'g_'; $chars['ķ'] = 'k_'; $chars['ļ'] = 'l_'; $chars['ņ'] = 'n_'; $chars['š'] = 's_'; $chars['ž'] = 'z_'; return $chars; }
You can extend the list of characters as needed.
Implementation:
1. Add the code snippet to a file outside the plugin folder. For example:
- Inside the functions.php
file of your theme.
- Or, use a plugin designed for adding custom code snippets.
2. After adding the code, re-index the Advanced Woo Search plugin's database table.
This ensures the search results are adjusted to handle Baltic characters correctly.