Skip to content

Instantly share code, notes, and snippets.

@n7studios
Last active November 23, 2023 21:55
Show Gist options
  • Save n7studios/9a4a0bea1de84c95cc625914b3ae6baf to your computer and use it in GitHub Desktop.
Save n7studios/9a4a0bea1de84c95cc625914b3ae6baf to your computer and use it in GitHub Desktop.
WordPress: Add the aria-label attribute to wp_dropdown_categories(): https://www.n7studios.co.uk/adding-aria-labels-wp_dropdown_categories/
<?php
/**
* If the aria-label argument has been specified in a wp_dropdown_categories() call,
* output the aria-label option in the <select>
*
* @since 1.0.0
*
* @param string $output HTML Output
* @param array $arguments wp_dropdown_category() arguments
* @return string Modified HTML Output
*/
function wp_dropdown_cats_aria_label_support( $output, $arguments ) {
// Bail if no aria-label argument has been supplied
if ( ! isset( $arguments['aria-label'] ) ) {
return $output;
}
// Add aria-label
$output = str_replace( '<select ', '<select aria-label="' . sanitize_text_field( $arguments['aria-label'] ) . '"', $output );
// Return
return $output;
}
add_filter( 'wp_dropdown_cats', 'wp_dropdown_cats_aria_label_support', 10, 2 );
@EvilBozkurt
Copy link

Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment