Server-side hyphenation for WordPress with Syllable library. It works by inserting ­
or "soft hyphen" characters to your strings so browsers will hyphenate the words correctly for you.
- PHP 7 +
- WordPress 5.0 +
- (Optional) Timber 1.10.0 +
- (Optional) Polylang
- Download latest version on the Releases page.
- Upload plugin on the WordPress plugin admin page.
- Activate plugin.
- See section below on how to use the plugin in your theme
You can use the syllable_hyphenate
WordPress filter to hyphenate text.
echo '<h1>' . apply_filters('syllable_hyphenate', get_the_title($post)) . '</h1>';
If you are using Timber, you can also use the hyphenate
Twig filter,
<h1>{{ post.title | hyphenate }}</h1>
You can use the syllable_hyphenate_html
filter to hyphenate a HTML string. For example, let's say you want to hyphenate output of Heading Gutenberg blocks. You can do it with the following code:
add_filter('render_block', function ($content, $block) {
if ($block['blockName'] === 'core/heading') {
$content = apply_filters('syllable_hyphenate_html', $content);
}
return $content;
}, 10, 2);
syllable_hyphenator_min_word_length
By default, minimum word length for hyphenation is set to 12 characters. This prevents awkward hyphenation of very short words.
add_filter('syllable_hyphenator_min_word_length', function () {
return 6;
});
syllable_hyphenator_wp_locale
Language used for hyphenation. By default this is the language specified in the WordPress settings page. This can be overridden using this filter. The locale should be in WordPress locale format, for example en_US
.
add_filter('syllable_hyphenator_wp_locale', function () {
return 'en_US';
});
syllable_hyphenator_current_locale
This overrides the WordPress language. This is useful if you have different language content on your site. If Polylang is installed, the current Polylang post language will be used. Return value should be in Syllable library format, for example en-us
.
add_filter('syllable_hyphenator_current_locale', function () {
return 'en-us';
});