Create a custom merge tag with modifier

Hi,

I’d like to create a custom merge tag with modifier.

I’ve found and adapted a code to create a merge tag and it’s working well

Here’s my code :

add_filter('gform_custom_merge_tags', 'custom_merge_tags', 10, 4);
function custom_merge_tags( $merge_tags, $form_id, $fields, $element_id ) {
	$merge_tags[] = array('label' => 'conclusion', 'tag' => '{conclusion}');
    return $merge_tags;
}
add_filter( 'gform_replace_merge_tags', 'replace_conclusion', 10, 7 );
function replace_conclusion( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
 
    $custom_merge_tag = '{conclusion}';
 
    if ( strpos( $text, $custom_merge_tag ) === false ) {
        return $text;
    }
	$html = '<p><strong>some html</strong></p>';				
    $text = str_replace( $custom_merge_tag, $html, $text );
    return $text;
}

I’d like to add a modifier to add a variable in the variable $html.
Something like :

$html = '<p><strong>some html ' . $modifier . ' </strong></p>';

How to do that ?

Regards

To add a modifier to your custom merge tag, you can modify the replace_conclusion function to parse the modifier value from the merge tag and use it to construct the final HTML output.

Here is an updated version of your code with a modifier added:

add_filter('gform_custom_merge_tags', 'custom_merge_tags', 10, 4);

function custom_merge_tags( $merge_tags, $form_id, $fields, $element_id ) {
    $merge_tags[] = array('label' => 'conclusion', 'tag' => '{conclusion:modifier}');
    return $merge_tags;
}

add_filter( 'gform_replace_merge_tags', 'replace_conclusion', 10, 7 );

function replace_conclusion( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
    preg_match('/{conclusion:(.*?)}/', $text, $matches);
    if (empty($matches)) {
        return $text;
    }
    $modifier = $matches[1];
    $html = '<p><strong>some html ' . $modifier . '</strong></p>';           
    $text = str_replace( $matches[0], $html, $text );
    return $text;
}

In the custom_merge_tags function, we modified the tag to {conclusion:modifier} to indicate that this tag can take a modifier value. In the replace_conclusion function, we used the regular expression preg_match to extract the modifier value from the merge tag. This value was then used to generate the final HTML output.

Hi Faisal,

Thank you, this is perfect.

Best regards

Hi Yoann,
Great to hear that the issue has been resolved! If you require any additional assistance in the future, please do not hesitate to open a new ticket.

Thank you,
Faisal