Terence Eden. He has a beard and is smiling.
Theme Switcher:

How to use the new <search> element with WordPress

· 3 comments · 150 words · Viewed ~265 times


There's a new HTML element in town! You can now use <search> to semantically mark up a search box. It's great for letting screen-readers and other assistive tech know what a form does.

It's only supported in WebKit for now - other browsers will get it eventually.

The WordPress default search widget hasn't yet updated, but I'm going to show you how you can start using it today!

In your theme, create a new file called searchform.php - WordPress will automatically load it for the search widget.

Add the following to the file:

 HTML<search role="search">
    <form method="get" id="searchform" class="searchform" action="<?php echo get_site_url(); ?>">
        <label class="screen-reader-text" for="s">Search for:</label>
        <input type="search" value="" name="s" id="s">
        <input type="submit" id="searchsubmit" value="Search">
    </form>
</search>

Save that and upload it to your theme's directory.

That's it!

Once the element is widely supported, you can drop the role="search".

Enjoy!


Share this post on…

3 thoughts on “How to use the new <search> element with WordPress”

  1. TIL about the element — nice 🙂

    Is there any particular reason to avoid using in favour of type="text" in this context?

    Reply

  2. Ack, I seem to have hit a filter! I meant to ask, why not use <input type="search" /> in addition to <search> …

    Reply

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

See allowed HTML elements: <a href="" title="">
<abbr title="">
<acronym title="">
<b>
<blockquote cite="">
<br>
<cite>
<code>
<del datetime="">
<em>
<i>
<img src="" alt="" title="" srcset="">
<p>
<pre>
<q cite="">
<s>
<strike>
<strong>

To respond on your own website, write a post which contains a link to this post - then enter the URl of your page here. Learn more about WebMentions.