Last active
January 12, 2017 16:06
-
-
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.
This file contains 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
// 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