Skip to content

Instantly share code, notes, and snippets.

@panosru
Last active January 3, 2024 16:47
Show Gist options
  • Save panosru/f2d3d6f5008958ae81b71510800be108 to your computer and use it in GitHub Desktop.
Save panosru/f2d3d6f5008958ae81b71510800be108 to your computer and use it in GitHub Desktop.
Dynamic population of ACF fields in VC and Templatera (BETA)
<?php
add_filter( 'the_content', 'remove_fontawesome', 99 );
function remove_fontawesome($content) {
wp_dequeue_style( 'tt-font-awesome' );
wp_dequeue_style('dhvc-woo-font-awesome');
wp_dequeue_style('dhvc-woocommerce-page-awesome');
return $content;
}
function check_if_original_post() {
global $original_id, $original_post_type;
// Check if templatera changed post id
if ((0 < (int)$original_id) && ($original_id != get_the_ID())) {
// Set original ID
$my_query = new WP_Query( array( 'post_type' => $original_post_type, 'p' => $original_id ) );
WPBMap::addAllMappedShortcodes();
$my_query->the_post();
}
}
function parse_escaped_acf_tags($content) {
// Check if "`{`acf" appears in text
if (false !== strpos($content, '`{`acf')) {
// Find converted ACF shortcodes and bring them back to normal
preg_match_all('/`{`acf[^}]*}`/i', $content, $matches);
// Set to current
$evaluated = $matches = current($matches);
// Loop through matched shortcodes
foreach ($evaluated as &$value) {
$value = do_shortcode(str_replace(
array ('`{`', '`}`', '``'),
array ( '[', ']', '"'),
$value
));
}
// Replace matched with values
$content = str_replace( $matches, $evaluated, $content );
}
return $content;
}
add_shortcode('acf_gallery', function ($atts, $content = null) {
// Extract Vars
extract(shortcode_atts(array(
'field' => '',
'serialised' => false
), $atts));
// Taking care of cache
ob_start();
// Check we are in original post
check_if_original_post();
if (filter_var($serialised, FILTER_VALIDATE_BOOLEAN)) {
$ids = get_field($field, false, false);
return is_array($ids)?implode(',', $ids):'';
}
return 'gallery';
});
add_shortcode('acf_repeater', function ($atts, $content = null) {
// Extract Vars
extract(shortcode_atts(array(
'field' => '',
'sub_field' => '',
'el_class' => '',
'format_output' => true
), $atts));
// Taking care of cache
ob_start();
// Check we are in original post
check_if_original_post();
// Check if the repeater field has rows of data
if (have_rows($field)) {
// Get our sub fields (trim each line and remove empty lines)
$sub_field = array_filter(array_map('trim', explode(PHP_EOL, $sub_field)), 'strlen');
$output = (filter_var($format_output, FILTER_VALIDATE_BOOLEAN))?'<div class="' . $el_class . '">':'';
// loop though the rows of data
while (have_rows($field)) {
the_row();
$search = $replace = array();
array_walk($sub_field, function ($value) use (&$search, &$replace) {
array_push($search, '${'.$value.'}');
array_push($replace, get_sub_field($value));
});
$output .= str_replace($search, $replace, $content);
}
if (filter_var($format_output, FILTER_VALIDATE_BOOLEAN)) $output .= '</div>';
}
return do_shortcode($output);
});
// Add to VC
vc_map(array(
'name' => __('ACF Repeader', 'my-shortcode'),
'base' => 'acf_repeater',
'as_parent' => array('except' => 'TS_VCSC_Animation_Frame'),
'content_element' => true,
'show_settings_on_create' => true,
'controls' => 'full',
'is_container' => true,
'container_not_allowed' => false,
'class' => '',
'params' => array(
array(
'type' => 'textfield',
'heading' => __('Repeater field name', 'my-shortcode'),
'param_name' => 'field',
'description' => __('Enter ACF repeater field name', 'my-shortcode')
),
array(
'type' => 'textarea',
'heading' => __('Repeater subfields name', 'my-shortcode'),
'param_name' => 'sub_field',
'description' => __('Enter ACF repeater subfield name (1 per line)', 'my-shortcode')
),
array(
"type" => "switch_button",
"heading" => __( "Format output", "my-shortcode" ),
"param_name" => "format_output",
"value" => "true",
"description" => __( "Format output with extra html", "my-shortcode" ),
"group" => "Other Settings",
),
array(
"type" => "tag_editor",
"heading" => __( "Extra Class Names", "my-shortcode" ),
"param_name" => "el_class",
"value" => "",
"description" => __( "Enter additional class names for the element.", "my-shortcode" ),
"group" => "Other Settings",
)
),
"js_view" => 'VcColumnView',
));
if (class_exists('WPBakeryShortCodesContainer')) {
class WPBakeryShortCode_Acf_Repeater extends WPBakeryShortCodesContainer {};
}
add_filter('the_content', function ($content) {
global $original_id, $original_post_type;
// Check if templatera shortcode exists
if (has_shortcode($content, 'templatera')) {
// Get current page id and post type and store it globally
$original_id = get_the_ID();
$original_post_type = get_post_type();
//$content = do_shortcode($content);
// Find templatera tags
$pattern = get_shortcode_regex(array('templatera'));
preg_match_all("/{$pattern}/", $content, $matches);
// Set to current
$evaluated = $matches = current($matches);
// Loop though matched templatera shortcodes
foreach ($evaluated as &$value) {
$atts = shortcode_parse_atts(str_replace(']', ' ]', $value));
if (key_exists('id', $atts)) {
$value = do_shortcode(parse_escaped_acf_tags(get_post_field('post_content', $atts['id'])));
}
}
// Replace matched with values
$content = str_replace($matches, $evaluated, $content);
// Unset variables
unset($evaluated, $matches, $atts, $pattern);
}
return parse_escaped_acf_tags($content);
});
@whoopcg
Copy link

whoopcg commented Feb 12, 2017

Hi there!
How can I use this?
Where to add it and how to call it?
Thanks in advance.

@ganonbit
Copy link

+1

@alessandropeana
Copy link

Hi there, i have the same problem... templatera doesn't see the acf. How can i use this code in order to solve my problem? Where to add it and how to call it? What mean +1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment