Articles

Use Shortcode’s with SCF (Secure Custom Fields)

0
0

An hour of digging through documentation, reddit, and blogs, I ended up having to watch a stupid video to get this function.

RANT: Secure Custom Fields disables short codes by default now. Who knew, I hardly use it, but it was frustrating trying to find this for sure. I would think this would be a toggle or setting, but I know that UX isn’t a strong skill set for the Automatic people MATT.

So anyways, add this to your functions to make SCF an actual useful plugin again.



function acf_field_shortcode($atts) {
  $atts = shortcode_atts(array(
    'field' => '',
    'post_id' => false,
  ), $atts, 'acf');

  if (function_exists('get_field')) {
    $field_value = get_field($atts['field'], $atts['post_id']);
    if (is_array($field_value)) {
      return esc_html(print_r($field_value, true));
    } else {
      return esc_html($field_value); 
    }  
  }

  return '';
}
add_shortcode('acf', 'acf_field_shortcode');