Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eclectic-coding/8e45e617c4646ea63c0d0b4fe74764da to your computer and use it in GitHub Desktop.
Save eclectic-coding/8e45e617c4646ea63c0d0b4fe74764da 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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment