Fix WordPress search widget (x)html validation errors

As it turns out, the standard WordPress issued search widget generates xhtml validation errors, by using the form role keyword.

For reason way beyond me, WordPress staff can find more time tracking the issue, changing it to a “future release” milestone, then decide to change the status from “new” to “closed”, set the resolution to “wontfix” (what a great resolution!), and then remove the milestone “Future Release”, and add the bold comment: “we’ll reopen if/when we’re interested.” source: WordPress Trac

I’m sure it must have been easier to remove the erroneous code: role="search" from the function that generates the search widget.

Here’s a quick trick to avoid these validation errors

Create your own search form

By creating your own searchform.php, you override the one WordPress uses by default, so it’s an easy fix. Using a text editor, like notepad, create a new file, and simply insert the following code:

[php]<form method="get" class="search-form" id="search-form" action="<?php bloginfo( ‘home’ ); ?>/">
<div>
<label class="screen-reader-text" for="s"><?php _e(‘Search for:’); ?></label>
<input class="search-text" type="text" name="s" id="search-text" value="<?php _e(‘Search this site’); ?>" />
<input class="search-submit" type="submit" name="submit" id="search-submit" value="<?php _e(‘Search’); ?>" />
</div>
</form>
[/php]

then save this file in your theme folder, and name it searchform.php.

From now on, your custom searchform will be used by the widget, and there will be no more validation errors.

Leave a Reply