Permalink

6

Fix role attribute in WordPress Search Widget

While validating a WordPress theme, I’veĀ  noticed that the default search widget outputted a role HTML attribute in the form tag that wasn’t valid in the XHTML 1.0 specification:

<form role="search" method="get" id="searchform" action="">
[...]
</form>

As for the spec, role is an attribute introduced in XHTML draft for accessibility, but still not valid.

If you need valid documents, the fastest way to get rid of this attribute is to edit the get_search_form() function in general-template.php core file, but for better compatibility and safer future upgrades you can target the issue from your theme’s functions.php file by adding the following lines:

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

 

6 Comments

  1. I love you! Thank you for this you have made my life so much easier. I have to make a theme XHTML valid for someone and you have fixed the last problem.

    Great Job!

Leave a Reply

Required fields are marked *.