Skip to content

Server-side hyphenation for WordPress with Syllable library.

License

Notifications You must be signed in to change notification settings

joppuyo/syllable-hyphenator

Repository files navigation

Syllable Hyphenator

Build Status Inactively Maintained

Screenshot of two WordPress pages. Normal on the left, hyphenated on the right

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.

Compatiblity

  • PHP 7 +
  • WordPress 5.0 +
  • (Optional) Timber 1.10.0 +
  • (Optional) Polylang

Installation

  1. Download latest version on the Releases page.
  2. Upload plugin on the WordPress plugin admin page.
  3. Activate plugin.
  4. See section below on how to use the plugin in your theme

Usage

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);

Filters

Minimum word length

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;
});

WordPress locale

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';
});

Current language

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';
});