You are looking for pre_get_posts
. This action is run between the composition of a (search) query and the actual query, to allow you to change the query. So, it would allow you to intercept banned keywords like this:
add_action( 'pre_get_posts', 'wpse338558_intercept_banned_keywords' );
function wpse338558_intercept_banned_keywords ($query) {
$banned = array ('word1','word2','word3');
if (in_array ($query->query_vars['s'], $banned)) $query->s="";
}
Notes:
- The above will simply empty the search, so there will be no feedback to the user. A hackish solution would be to use a very obscure search term like
jlwenuhrofslslfiehlserlisehrlilfheeipeueiq
and intercept that 404 to display a message. - The above will only intercept single keywords. If someone searches for two banned keywords at the same time he will get through. So you’ll need a more elaborate condition to make this foolproof.