WordPress Tutorial: Outputting Search Terms
Here is a quick fix for outputting WordPress search parameters within form fields such as search input boxes.
Just add the following snippet to your functions.php file:
function new_get_search( $escaped = true ) {
$query = urldecode(get_query_var( 's' ));
if ( $escaped ){
$query = esc_attr( $query );
}
return $query;
}
add_filter( 'get_search_query', 'new_get_search');Then when used with a standard WordPress searchform.php file, the output is rendered in natural language without the extra query string variables. Example:
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<div class="form-search">
<label for="search">Search:</label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s"
id="s" size="20" onfocus="this.value=''" class="text" />
<button type="submit" title="Search" class="button"><span><span>Search</span></span></button>
</div>
</form>So when searching for test test the search input field will display test test instead of test+test.
Looking for Expert WordPress or Drupal development?
I am a freelance website developer and designer based in the UK. I work as a remote Drupal developer, WordPress developer and Front-end developer for a variety of startups, charities and international businesses in Bristol, Bath, London and Europe. You can learn more about me by visiting my resume page.
Get in touch for a free quote on your next project and if you want to connect me with then check out my LinkedIn profile.