Skip to content

Instantly share code, notes, and snippets.

@qichunren
Created February 18, 2021 06:34
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 qichunren/0e9516430ef6b11fd81172ec8f0186e7 to your computer and use it in GitHub Desktop.
Save qichunren/0e9516430ef6b11fd81172ec8f0186e7 to your computer and use it in GitHub Desktop.
wordpress notes
  • action
  • filter
function print_initials( $name ) {
if ( ! is_string( $name ) ) {
return;
}
$fragments = explode( ' ', $name );
/**
* Filter wether to print initials in reverse order.
*
* @param bool $reverse Print initials in reverse order?
*/
if ( apply_filters( 'reverse_initials', FALSE ) ) {
$fragments = array_reverse( $fragments );
}
foreach ( $fragments as $f ) {
echo substr( $f, 0, 1 );
}
}
print_initials( 'Some Guy' ); // outputs: SG
add_filter( 'reverse_initials', '__return_true' );
print_initials( 'Some Guy' ); // outputs: GS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment