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.