Disclaimer: This snippets code is from https://github.com/seemly/shortcode-extended.
Shortcode: [ acf field=”you-custom-field” ]
- Copy the code below and you can paste it on the functions.php
if(!class_exists('ShortcodeExtended'))
{
class ShortcodeExtended
{
public function render($attributes, $content, $data)
{
// just a failsafe
if(!($data instanceof WP_Block))
{
return $content;
}
// if no ACF not activated or installed, then return early.
if(!function_exists('get_field'))
{
return $content;
}
// if no ACF shortcode found in content, then return early.
if(strpos($content, '[acf ') === false)
{
return $content;
}
// Native functionality is to call `wpautop`, which means we lose access to the Post ID and ACF related data
return do_shortcode($content);
}
}
add_filter('register_block_type_args', function ($args, $name)
{
// Here we list the native blocks we are likely to include ACF shortcodes in.
// This list probably needs to be expanded, but suits my immediate requirements.
$validBlocks = ['core/shortcode', 'core/paragraph', 'core/list'];
// override the native render_callback function to ensure ACF shortcodes run as expected.
if(in_array($name, $validBlocks))
{
$args['render_callback'] = [new ShortcodeExtended, 'render'];
}
return $args;
}, 10, 2);
}