0

I would like to establish a minimum number of characters in the search. On sites that have many entries, spammers tend to type very short terms as "a" or "of", and the server collapses while trying to return the results. Therefore, I would like to establish the minimum number of characters for the search to be made​​, for example, three characters.

Relevanssi plugin seems to do this. But I would do without using any plugins.

Thanks in advance.

  • Why would spammers search in the first place? You meant to take your site down (like DDOS)? – its_me Jun 23 '12 at 10:25
  • Sorry @Krish Aahan, this is not DDOS. Actually the server does not fall, just takes longer to return results or gives error. – BóbGCA Jun 23 '12 at 10:34
  • This is impossible without using the plugin API. Relevanssi is probably the best solution for your problem. Your question doesn’t make much sense in its current form. Please improve it. – fuxia Jun 23 '12 at 15:32
  • Relevanssi is the worst you can use on a busy site, the queries are slow. Even the creator of relevanssi advices not to use it on big sites. the ideal would be 3rd party search like Algolia but it's very expensive. Google custom search is an option, wordpress.org uses it but it's limited. – Michael Rogers Mar 31 '17 at 17:25
1

Quick and dirty, put this before your get_header() in your search.php

<?php
// Get the query string
$query = get_search_query();
// if the first & last char is space, rip them
$query = trim($query);
// if there are more than one space, rip to one space
$query = preg_replace('/\s\s+/', ' ',$query);
// if chars count is less than  3, redirect them to homepage
if (strlen($query)<3){
wp_redirect( home_url() ); 
exit; 
}
?>
  • Thanks, @Gembel Intelek. Your code Quick and dirty work. Just is not working in some words that have accent. Ex: "fe" (faith in English), no redirects. Faith (with accent on "e"), redirects correctly. "fé" would be the correct way to write the word in Portuguese, although many write without an accent. – BóbGCA Jun 24 '12 at 5:23
  • How about displaying an error explaining what happened preferably on search.php. Else people will not know what happened if you redirect them like that. – Michael Rogers Mar 22 '17 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.