Skip to content

Instantly share code, notes, and snippets.

@n7studios
Last active July 25, 2017 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n7studios/2f432dd774180ef8428db1c4d8472ce7 to your computer and use it in GitHub Desktop.
Save n7studios/2f432dd774180ef8428db1c4d8472ce7 to your computer and use it in GitHub Desktop.
Add aria-label to 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
*/
public 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 );
<?php
/**
* Example usage of wp_dropdown_categories(), when aria-label
* support has been added.
*
* @since 1.0.0
*/
wp_dropdown_categories( array(
// Other wp_dropdown_categories() arguments go here...
// Define our aria-label
'aria-label' => __( 'ARIA Label Value', 'domain' ),
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment