Die Template Tags können mittels {{$prefixes.fieldname}} hinzugefügt werden.
Wenn du z.B eine Optionsseite hast und dort drin auf erster Ebene ein ACF Field. Dann kannst du den Code {{option.FELDERSTEEBENE}} verwenden.
Wenn du eine Optionsseite hast und von dort ein Feld aus einer zweiten Ebene anzeigen lassen möchtest, geht das so: {{option.FELDERSTEEBENE_FELDZWEITEEBENE}}.
Also bspw. {{option.business_mail}} oder {{field.subheading}}
{{year.current}} gibt das gegenwärtige Jahr aus.
{{year.2012_current}} gibt 2012 – das gegenwärtige Jahr aus.
PHP
<?php
add_filter("render_block", "gw_template_tags", 10, 2);
/**
* Process dynamic template tags in block content
*
* @param string $block_content The block content to process
* @param array $block The block data
* @return string The processed block content
*/
function gw_template_tags($block_content, $block)
{
// Don't process if no template tags exist (early return for performance)
if (!str_contains($block_content, '{{')) {
return $block_content;
}
// Supported tag prefixes
$prefixes = [
"option",
"field",
"year",
];
// Compile the pattern once
static $pattern = null;
if ($pattern === null) {
$pattern = '/{{(' . implode('|', $prefixes) . ')\.([a-zA-Z0-9_-]+)}}/';
}
// Process href attributes separately (to handle space removal and tel links)
$block_content = preg_replace_callback(
'/href="([^"]*{{(?:' . implode('|', $prefixes) . ')\.[^"]+}}[^"]*)"/i',
function ($matches) use ($pattern) {
$href_content = preg_replace_callback(
$pattern,
function ($href_matches) {
return str_replace(' ', '', gw_process_dynamic_tag($href_matches[1], $href_matches[2]));
},
$matches[1]
);
// Check if this is a telephone link with +41 (Switzerland) and strip first 0 after country code
if (preg_match('/^tel:\+41(0\d+)(.*)$/', $href_content, $tel_matches)) {
// Remove the first 0 after the country code
$href_content = 'tel:+41' . substr($tel_matches[1], 1) . $tel_matches[2];
}
return 'href="' . esc_url($href_content) . '"';
},
$block_content
);
// Process other content
$block_content = preg_replace_callback(
$pattern,
function ($matches) {
return esc_html(gw_process_dynamic_tag($matches[1], $matches[2]));
},
$block_content
);
return $block_content;
}
/**
* Process a single dynamic tag
*
* @param string $prefix The tag prefix (option, field, year)
* @param string $tag The tag name
* @return string The processed value
*/
function gw_process_dynamic_tag($prefix, $tag)
{
// Sanitize the tag name to prevent injection
$tag = sanitize_key($tag);
// Default empty value
$value = '';
switch ($prefix) {
case 'option':
// Check for ACF and get option
if (function_exists('get_field')) {
$value = get_field($tag, 'options');
}
break;
case 'field':
// Check for ACF and get field
if (function_exists('get_field')) {
$value = get_field($tag);
}
break;
case 'year':
// Get current year
$current_year = wp_date('Y');
// Handle the tag format
if ($tag === 'current') {
// Just return the current year
$value = $current_year;
} elseif (preg_match('/^(\d{4})_current$/', $tag, $matches)) {
// Format as [start_year] - [current_year]
$start_year = $matches[1];
// Validate start year (must be 4 digits and not in the future)
if (strlen($start_year) === 4 && $start_year <= $current_year) {
$value = $start_year;
// Only add the range if years are different
if ($start_year != $current_year) {
$value .= ' - ' . $current_year;
}
}
}
break;
}
// Ensure we always return a string
return is_scalar($value) ? (string) $value : '';
}