Valid WordPress Search Widget

Follow @tristarweb

You may have noticed with WordPress, that the search widget doesn’t validate due to the role attribute. For example:

<form role="search" method="get" id="searchform" action="">
// Form Code
</form>

If you require a valid XHTML document you will need to remove this attribute by editing the get_search_form() function in the general-template.php core file. However, if you update your WordPress installation this file may get overridden. A safer method is to add the following code to your themes functions.php file.

function valid_search_form ($form) {
    return str_replace('role="search" ', '', $form);
}
add_filter('get_search_form', 'valid_search_form');

What we have done here is created a new function called valid_search_form() which accepts one value. The function does a simple string replace that removes role=”search” from the $form variable and replaces it with an empty string.

In order for this to work the function needs to be triggered. To do this we have used the ‘add_filter’ hook which will force the search widget to be passed through the function valid_search_form ($form) before being displayed the users.

Save and upload functions.php – Now when you reload your website design and view the source code – the widget should look something like:

<form method="get" id="searchform" action="">
// Form Code
</form>

Resubmit your WordPress page to the validator and hopefully you should see a nice green bar saying ‘Passed’. I hope you find this wordpress tip useful.

About robert

Hi my name is Rob Hills. At most times you will find me playing around with Magento and Wordpress. I am always looking to improve my web development knowledge – this ranges from PHP, Javascript, jQuery, XML etc. Other than web development – I produce Dance Music. My biggest achievement is getting my track played by Kutski on Radio 1..

Be Social:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter