How to use the new <search> element with WordPress


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 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…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

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

  1. Eva Lauren says:

    TIL about the element — nice 🙂

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

    Reply
  2. Eva Lauren says:

    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.

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