Skip to content

Instantly share code, notes, and snippets.

@shoreline-chrism
Created April 7, 2021 18:43
Show Gist options
  • Save shoreline-chrism/6a95b9d310aa4bf6a219258f1b280ce4 to your computer and use it in GitHub Desktop.
Save shoreline-chrism/6a95b9d310aa4bf6a219258f1b280ce4 to your computer and use it in GitHub Desktop.
Convert text on separate lines to <li> items. Useful for custom fields where you want the user to input things on separate lines, but want them as a bullet list on frontend. In ACF, set the field type to 'wysiwyg' and use get_field() to get the text without HTML formatting by passing false to last argument.
<?php
// Convert text on separate lines to li items
if ( !function_exists( 'sl9_text_to_li' ) ) {
function sl9_text_to_li( $text ) {
global $shortcode_tags;
$pattern =
'/'
. '((?!\R).+\R?)' // All lines of text
. '/m';
return preg_replace( $pattern, '<li>$1</li>', $text );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment