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

When users search for 1,2 or 3 characters this puts a lot of strain on the database. A malicious user could search for 1,2 or 3 characters and bring my site down to its knees.

I presume I could use some Jquery code to prevent users entering less than 4 characters when searching.

   <form method="get" id="searchform" action="<?php echo home_url(); ?>/">                             
                            <input type="text" value="<?php _e( 'search', 'swagger' ); ?>" onfocus="if (this.value == '<?php _e( 'search', 'swagger' ); ?>') {this.value = '';}" onblur="if (this.value == '') {this.value = '<?php _e( 'search', 'swagger' ); ?>';}" name="s" id="s" />          

any suggestions?

share|improve this question

You can use the pattern attribute to ask for at least 3 characters:

<input pattern=".{3,}">

But if your database dies just because someone searches for a three letter word … you have other problems than search. Optimize your database instead, or find another solution. A simple search, even dozens per minute should not be a problem.

To catch empty searches, see my article on WPEngineer: Fix Empty Searches

share|improve this answer
ok this works but it doesn't prevent users entering nothing and pressing enter – Mario Lorenzo May 23 '13 at 21:47
@MarioLorenzo See my update. – toscho May 23 '13 at 21:51

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.