Replace search keywords (whole words)
In case you need to replace certain whole words that the user enters to others (like synonyms), then use this custom code.
1
add_filter( 'asp_query_args', 'asp_replace_search_keywords', 10, 2 );
2
function asp_replace_search_keywords( $args, $id ) {
3
/**
4
* Add as many words as you want. On the left side place the
5
* word or words separated by comma to replace.
6
* To the right side, place the single word to replace it with.
7
*/
8
$replace = array(
9
'word1' => 'word2', // Replace 'word1' with 'word2'
10
'word3, word4' => 'word5' // Replace 'word3' and 'word4' with 'word5'
11
);
12
13
// ----------------------------------------
14
// Do not change anything below this line
15
// ----------------------------------------
16
foreach ( $replace as $k => $w ) {
17
$kr = explode(',', $k);
18
foreach ($kr as $krk => &$krv) {
19
$krv = trim($krv);
20
if ( $krv == '' ) {
21
unset($kr[$krk]);
22
} else {
23
$krv = '/\b' . $krv . '\b/u';
24
}
25
}
26
if ( count($krv) > 0 ) {
27
$args['s'] = preg_replace($kr, $w, $args['s']);
28
}
29
}
30
return $args;
31
}
Copied!
Follow the instructions within the code to add/remove lines to the $replace array variable. Each key is a keyword or keywords separated by comma, and each value is the substitue keyword.
Last modified 1yr ago
Copy link