Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Ignoring special characters
This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko 3 years, 9 months ago.
- AuthorPosts
- June 25, 2020 at 10:38 am #28080
Hi Ernest,
Please let me describe a bit about my requirements as we talked earlier:
In my Woocommerce website I have products that have special characters in their titles, for example:
Dīvān-i ashʻār-i Jalāl Ṭabīb Shīrāzī / Naṣr Allāh Pūrjavādī
I would like the visitors to be able to find the above product using any of the following search phrases:
divani
ashari
tabib
nasr
purjavThe html character codes I sent to you in my previous message were related to the special characters you see on the top and bottom of some the normal characters of the above-mentioned title example (īʻāṬṣāūāī)
If you guarantee that you will solve this for me, I will buy your plugin immediately.
Kind regards,
MajidJune 25, 2020 at 1:11 pm #28087Hi Majid,
So, my suggestion is using a custom code with the index table engine. The custom code will try to replace these characters with the single unicode versions on both the server and client side.
Please follow the instructions below:
1. Please add this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter('asp_indexing_string_pre_process', 'custom_chars_asp_indexing_string_pre_process', 10, 1); add_filter('asp_search_phrase_before_cleaning', 'custom_chars_asp_indexing_string_pre_process', 10, 1); add_filter('asp_query_args', 'custom_chars_asp_indexing_string_pre_process', 10, 1); function custom_chars_asp_indexing_string_pre_process($s) { $original = array( 'ī', 'ʻ', 'ā', 'Ṭ', 'ṣ', 'ū' ); $replace = array( 'i', '', 'a', 't', 's', 'u' ); // Replace them if ( is_array($s) ) { if ( isset($s['s']) && !$s['_ajax_search'] ) $s['s'] = str_replace($original, $replace, $s['s']); } else { $s = str_replace($original, $replace, $s); } return $s; }
2. Make sure to configure the index table engine. Follow the tutorial.
3. Once ready and the indexing is finished. Make sure the index table is enabledIf all goes right, entering the words should yield results. The code will only replace the given characters, only the lowercase versions. For this to work with the upper case, it needs to be added as well.
- This reply was modified 3 years, 9 months ago by Ernest Marcinko. Reason: Fixed code
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 25, 2020 at 1:24 pm #28089Hi Ernest,
Thank you for the reply.
The search engine was already set to index table.
I created a snippet containing your code.
I deleted the index.
I clicked on “Create new index” button, but it indexed only one item. (problem!)
I disabled your code snippet.
I clicked on “Create new index” button, then all items were indexed successfully as before.It seems the code snippet prevents indexing.
Any comment please?Best,
MajidJune 25, 2020 at 1:26 pm #28090Hi Majid,
Indeed, there is an error in the code, please try this variation:
Best,add_filter('asp_indexing_string_pre_process', 'custom_chars_asp_indexing_string_pre_process', 10, 1); add_filter('asp_search_phrase_before_cleaning', 'custom_chars_asp_indexing_string_pre_process', 10, 1); add_filter('asp_query_args', 'custom_chars_asp_indexing_string_pre_process', 10, 1); function custom_chars_asp_indexing_string_pre_process($s) { $original = array( 'ī', 'ʻ', 'ā', 'Ṭ', 'ṣ', 'ū' ); $replace = array( 'i', '', 'a', 't', 's', 'u' ); // Replace them if ( is_array($s) ) { if ( isset($s['s']) && !$s['_ajax_search'] ) $s['s'] = str_replace($original, $replace, $s['s']); } else { $s = str_replace($original, $replace, $s); } return $s; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 25, 2020 at 1:37 pm #28092Ernest,
It seems it works!
I have added two other special characters, those worked as well 🙂I’m very happy now!
Let me check further.Best,
MajidJune 25, 2020 at 1:38 pm #28093That is really great to hear 🙂 It may not work with everything, but should do most of them.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 25, 2020 at 3:22 pm #28095Hi Ernest,
One issue regarding small and capital letters (case sensitivity):
If we use:
$original = array(
‘Ā’, ‘ā’
);
$replace = array(
‘A’, ‘a’
);Only search string “Ava” will find the post title “Āvā”. How to make it case insensitive so that it can also be found with search strings like: “ava” or “avA” ?
Regards,
MajidJune 26, 2020 at 7:26 am #28096Hi Majid,
Use the lower case for both in the replacement:
Best,$original = array( 'Ā', 'ā' ); $replace = array( 'a', 'a' );
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
The topic ‘Ignoring special characters’ is closed to new replies.