Last active
November 23, 2023 21:55
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Thank you so much