Skip to content

Instantly share code, notes, and snippets.

@jdelia
Last active January 12, 2017 16:06
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 jdelia/a9384337757ee3e82d3663ce95f6ab95 to your computer and use it in GitHub Desktop.
Save jdelia/a9384337757ee3e82d3663ce95f6ab95 to your computer and use it in GitHub Desktop.
Widget Title filter for WordPress. Allows screen reader text output, no title output options.
// Widget titles in brackets are either ignored or wrapped in screen reader text span
add_filter( 'widget_title', 'jd_flexible_widget_titles', 10, 1 );
/**
* Customize output of widget title
*
* @param string $widget_title return modified widget title.
*
* @author Jackie D'Elia
*/
function jd_flexible_widget_titles( $widget_title ) {
// get rid of any leading and trailing spaces
$title = trim( $widget_title );
// get the length of the string
$title_length = strlen( $title );
// check for [sr Title to Output] to indicate output for screen reader only
// else check the first and last character, if [ and ] set the title to empty
if ( $title_length > 3 ) {
if ( '[sr ' === substr( $title, 0, 4 ) && ']' === $title[ $title_length - 1 ] ) {
$title = '<span class="screen-reader-text">' . substr( $title, 4, $title_length - 5 ) . '</span>';
} else {
if ( '[' === $title[0] && ']' === $title[ $title_length - 1 ] ) {
$title = '';
}
}
}
return $title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment