Replace or remove characters from search phrase

By default the plugin allows searching any character without restriction. If you however need to replace (or remove) certain special characters, use the code below.

What is this, and where do I put this custom code?

add_filter('asp_search_phrase_before_cleaning', 'asp_replace_characters', 10, 1);
add_filter('asp_query_args', 'asp_replace_characters', 10, 1);

function asp_replace_characters( $s ) {
  $characters = "',._-?!"; // Type characters one after another
  $replace_with = ' ';     // Replace them with this (space by default)

  if ( is_array($s) ) {
    if ( isset($s['s']) && !$s['_ajax_search'] ) 
      $s['s'] = str_replace(str_split($characters), $replace_with, $s['s']);      
  } else {
    $s = str_replace(str_split($characters), $replace_with, $s);
  }

  return $s; 
}

Type the characters into the $characters variable, one after another. By default it replaces it with a space character. In case you want to remove those characters, change the

$replace_with = ' ';

with:

$replace_with = '';

Last updated

Copyright Ernest Marcinko