Archived topic
Add Label Element to Form Control
5 replies · Started by Jesse on January 5, 2021
Hello,
This line of HTML is output on every page twice:
<input type="search" class="search-field" value="" name="s" title="Search">
I need to assign the <label> element to that form control as described here:
https://www.w3.org/WAI/tutorials/forms/labels/
So basically, the output should look something like this:
<input type="search" class="search-field" value="" name="s" title="Search">
<label for="s">Search</label>
How can I accomplish this?
Best regards,
Jesse
Hi,
This line of HTML is output on every page twice:
It's rendered twice because the mobile header is enabled. Mobile header is its own independent header with its own search form so it will create its own <input> tag.
As for changing how the nav search output, generate_navigation_search_output filter should help.
Example: (adding <label for="s">Search</label> )
add_filter( 'generate_navigation_search_output', function() {
printf(
'<form method="get" class="search-form navigation-search" action="%1$s">
<input type="search" class="search-field" value="%2$s" name="s" title="%3$s" /> <label for="s">Search</label>
</form>',
esc_url( home_url( '/' ) ),
esc_attr( get_search_query() ),
esc_attr_x( 'Search', 'label', 'generatepress' )
);
});
Or edit the markup within the filter to your preference.
Hello,
I added that code as a snippet, and it crashed my site as soon as I saved it. I had to restore a previous backup to get back in. I was completely locked out due to a critical site error.
Why did this happen?
Regards,
Jesse
Hi there,
sorry about that i have corrected the function here:
https://generatepress.com/forums/topic/add-label-element-to-form-control/#post-1607470
Thanks, David. That worked!
Glad to hear that!