Set minimum number of characters in the search

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; 
}
?>

Leave a Comment