WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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. – toscho Jun 23 '12 at 15:32

Quick and dirty, put this before your get_header() in your search.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() ); 
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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