# Give Theme Search Forms an Accessible Label

> Adds an accessible name to the search field of theme search forms that have no label, so screen reader users hear "Search" instead of an unlabeled text field.

- Type: PHP
- Runs: Frontend only
- Category: [Accessibility](https://wpide.com/library/category/accessibility/)
- Tags: search, forms, labels
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/a11y-search-form-label/

## Code

```php
// Accessible name for the search field.
$wpide_search_label = 'Search this site';

add_filter( 'get_search_form', function ( $form ) use ( $wpide_search_label ) {
    if ( false !== stripos( $form, '<label' ) || ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
        return $form;
    }

    $tags = new WP_HTML_Tag_Processor( $form );

    while ( $tags->next_tag( 'input' ) ) {
        if ( ( 's' === $tags->get_attribute( 'name' ) || 'search' === $tags->get_attribute( 'type' ) ) && ! $tags->get_attribute( 'aria-label' ) ) {
            $tags->set_attribute( 'aria-label', $wpide_search_label );
        }
    }

    return $tags->get_updated_html();
}, 20 );
```

## Notes

Change $wpide_search_label to translate the label.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Give Theme Search Forms an Accessible Label" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
