The wp_sprintf_l
filter allows you to modify the default list item separators used in lists employing the %l
specifier — such as when using the the_taxonomies()
template tag. When we say “separators”, we’re talking about the punctuation and/or punctuation + words used to grammatically space items in a list.
For example:
- If there are two taxonomy terms, the separator would be ‘ and ‘, e.g. ‘Something and Something Else’
- If there are three or more terms, the separator ‘, ‘ would be used up until the last term, then the separator used would be ‘, and ‘, e.g. ‘Something, Something Else, and Something Else Again’
It is evaluated in wp_sprintf_l()
in the wp-includes/formatting.php file. wp_sprintf_l()
exists to make these default item separators localizable for different languages.
Important note: You should exercise caution modifying the default separators as translators use them as a guide.
wp_sprintf_l
accepts a single argument that takes the form of an array. Each key/value pair in the array specifies a different type of list item separator:
'between'
=>', '
'between_last_two'
=>', and '
'between_only_two'
=>' and '
Example:
Let’s say you’re one of those people who just can’t stand the serial/Oxford comma, the following will modify the between_last_two
key in the wp_sprint_l
separators array.
<?php | |
/** | |
* Remove use of Oxford/serial comma at the end of taxonomy lists | |
* | |
* @param array $separators The array of defaul list item separators. | |
* | |
* @return array The filtered list of list separators. | |
*/ | |
function wpdocs_no_oxford_comma( $separators ) { | |
// Replaces __( ', and ' ) | |
$separators['between_last_two'] = __( ' and ' ); | |
return $separators; | |
} | |
add_filter( 'wp_sprintf_l', 'wpdocs_no_oxford_comma' ); |
View the code example on Gist.
Have a suggested improvement? You can fork the Gist and comment back with the link. If all is agreeable, I’ll merge in your changes. Crowd-sourced documentation FTW!