Skip to content

Instantly share code, notes, and snippets.

@kyleturman
Last active December 11, 2016 16:36
Show Gist options
  • Save kyleturman/6971f1fe2ac6eb4935ff00009c3315e4 to your computer and use it in GitHub Desktop.
Save kyleturman/6971f1fe2ac6eb4935ff00009c3315e4 to your computer and use it in GitHub Desktop.
<?php
// Prevent Widows Kirby CMS plugin
// Makes line breaking text go from:
// Lorem ipsum dolor sit amet, consectetur adipiscing
// elit.
// To:
// Lorem ipsum dolor sit amet, consectetur
// adipiscing elit.
// Put in your site/plugins folder
field::$methods['kirbytext'] = field::$methods['kt'] = function($field) {
// Adds &nbsp; char before second to last word
// Regex from http://www.webmastertools.bz/simple-php-regex-script-to-prevent-orphans-in-paragraph-text/
$textNoWidows = preg_replace('/\s([\w]+[.,!\:;\\"-?]{0,1})$/', '&nbsp;\\1', $field->value);
// Runs kirbytext like normal
$field->value = kirbytext($textNoWidows);
return $field;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment