get_search_form() and aria_label

How do you pass the aria_label parameter to get_search_form()?

I can't seem to pass the 'aria_label' parameter to get_search_form() in the correct way and can't find any examples. Lots with the first parameter 'echo' which works as in the documentation, but nothing I've tried for aria_label has made the search form add aria labels.

Topic accessibility theme-development Wordpress

Category Web


Since WordPress 5.5 we have been able to pass data into template files and use that data in the template from the $args array. This was a "long awaited addition" according to the article.

You can read more about this here https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/ and this goes along with the info @toby-dawes gave above.

This is super powerful and I have been using this in my custom themes since it was added. Love it!


I ran into this similar issue, and was able to resolve in my searchform.php by adding this to my form tag:

<form aria-label="<?php echo $args['aria_label']; ?>">...</form>

This produces the output of what was passed into the aria_label item in the arguments array.


You can pass an array of attributes with aria_label attribute in it.
Try this:

get_search_form(array('aria_label' => 'search-form'));

This function gets a form from searchform.php by default and if you have this file in your theme and aria-label still missing, you need to check the code in this file. Sometimes developers do not include an option to set an aria-label.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.