Skip to content

Instantly share code, notes, and snippets.

@simonhughes
Created August 12, 2019 15:50
Show Gist options
  • Save simonhughes/739ec55518f67db98c241d579b8c603b to your computer and use it in GitHub Desktop.
Save simonhughes/739ec55518f67db98c241d579b8c603b to your computer and use it in GitHub Desktop.
WordPress: convert bbcode in ACF field messages and instructions to HTML markup.
/**
* Filter: convert bbcode into html markup for added emphasis to ACF fields.
*
* @note thanks to StackOverflow https://stackoverflow.com/questions/24200048/show-bbcode-in-html-page-with-php
* @return $field
*/
add_filter('acf/prepare_field', function($field){
// Are both viable fields empty; else: bail out.
if (empty($field['message']) && empty($field['instructions']))
return $field;
// Target patterns
$search = array (
'/(.*?)(\[br\])(.*?)/',
'/(\[b\])(.*?)(\[\/b\])/',
'/(\[i\])(.*?)(\[\/i\])/',
'/(\[u\])(.*?)(\[\/u\])/',
'/(\[ul\])(.*?)(\[\/ul\])/',
'/(\[li\])(.*?)(\[\/li\])/',
'/(\[url=)(.*?)(\])(.*?)(\[\/url\])/',
'/(\[url\])(.*?)(\[\/url\])/'
);
// Replacement markup
$replace = array (
'$1<br/>$3',
'<strong>$2</strong>',
'<em>$2</em>',
'<u>$2</u>',
'<ul>$2</ul>',
'<li>$2</li>',
'<a href="$2" target="_blank">$4</a>',
'<a href="$2" target="_blank">$2</a>'
);
// Replace message
if (!empty($field['message']))
$field['message'] = preg_replace($search, $replace, $field['message']);
// Replace instructions
if (!empty($field['instructions']))
$field['instructions'] = preg_replace($search, $replace, $field['instructions']);
// Return $field
return $field;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment