Header button dynamic values
This snippet lets you use dynamic values in Blocksy header buttons — things like a phone number or a link that change depending on the current page. You place a placeholder like {phone_number} directly in the button’s text or URL field in the customizer, and a filter supplies the actual value at render time.
Placeholder syntax
Section titled “Placeholder syntax”{key}— replaced with the registered value; left unchanged if no replacement is found{key|default value}— falls back to the inline default if no replacement is registered or if the replacement isnull
Example: a button label of Call us on {phone_number|+31 6 00 00 00 00} will render the registered phone number, or the hardcoded fallback if none is set.
Implementation
Section titled “Implementation”Add this snippet to your theme to enable placeholder replacement on all Blocksy header buttons. The check on line 3 limits this to button elements, but you can remove or adjust it to apply the same logic to other header elements.
<?php
/** * Replace placeholders in Blocksy header button attributes. * * Hooks into the Blocksy header element template args filter to perform * placeholder replacement on button text and link fields before rendering. * * Replacements are sourced from the 'blocksy-child/header/placeholder_replacements' * filter, and the fields to apply them to are sourced from the * 'blocksy-child/header/placeholder_fields' filter, allowing both to be * configured from anywhere in the theme. * * @see blocksy-child/header/placeholder_replacements * @see blocksy-child/header/placeholder_fields */add_filter('blocksy:header:item-template-args', function ($args) { if (strpos($args['item_id'], 'button') === false) { return $args; }
/** * Filters the placeholder replacements for Blocksy header buttons. * * Each key is a bare placeholder name (e.g. 'phone_number') and each value * is the string it should be replaced with. A null value is treated as * "not set", allowing an inline default (e.g. {phone_number|0612345678}) * to take over. An empty string is a valid replacement and will suppress * the inline default. * * @param array $replacements { * Associative array of placeholder names and their replacements. * * @type string|null $key The bare placeholder name, e.g. 'phone_number'. * @type string|null $value The replacement string, or null to fall back to the inline default. * } */ $replacements = apply_filters('blocksy-child/header/placeholder_replacements', []);
/** * Filters the button attribute fields that placeholder replacement is applied to. * * By default, replacement runs on the button text, link, secondary text, * and aria label fields. Use this filter to add or remove fields. * * @param string[] $fields Array of Blocksy button attribute keys. */ $fields = apply_filters('blocksy-child/header/placeholder_fields', [ 'header_button_text', 'header_button_link', 'header_button_secondary_text', 'header_button_aria_label', ]);
foreach ($fields as $field) { if (!empty($args['atts'][$field])) { $args['atts'][$field] = preg_replace_callback( '/\{([^}|]+)(?:\|([^}]*))?\}/', function ($matches) use ($replacements) { $key = $matches[1]; $default = $matches[2] ?? null;
if (array_key_exists($key, $replacements) && $replacements[$key] !== null) { return $replacements[$key]; }
if ($default !== null) { return $default; }
return $matches[0]; // leave placeholder unchanged }, $args['atts'][$field] ); } }
return $args;});Adding replacements
Section titled “Adding replacements”Use the blocksy-child/header/placeholder_replacements filter to register values for your placeholders. This filter runs on every header button render, so you can conditionally return different values based on the current post, page, or any other context.
The example below reads ACF fields from the current post and provides them as replacements. Adapt the post type check and field names to your own setup.
add_filter('blocksy-child/header/placeholder_replacements', function ($replacements) { if (!is_singular('garage')) { return $replacements; }
$post = get_the_ID();
return array_merge($replacements, [ 'phone_number' => get_field('phone_number', $post) ?: null, 'appointment_link' => get_field('appointment_url', $post) ?: null, ]);});Customizing which fields are processed
Section titled “Customizing which fields are processed”By default, replacement is applied to header_button_text, header_button_link, header_button_secondary_text, and header_button_aria_label. Use the blocksy-child/header/placeholder_fields filter to add or remove fields.
// Remove the link field from placeholder replacementadd_filter('blocksy-child/header/placeholder_fields', function ($fields) { return array_diff($fields, ['header_button_link']);});