Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Forked from hereswhatidid/gforms-merge-tag.php
Last active July 20, 2023 13:50
Show Gist options
  • Save FrancoStino/e78490d8054529ab830d91083e6a9084 to your computer and use it in GitHub Desktop.
Save FrancoStino/e78490d8054529ab830d91083e6a9084 to your computer and use it in GitHub Desktop.
Create a custom Gravity Forms merge tag
<?php
/**
* Add custom merge tag to the merge tags drop down in the form editor
*
* @param array $merge_tags
* @param int $form_id
* @param array $fields
* @param int $element_id
*
* @return array
*/
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' => 'Custom Merge Tags', 'tag' => '{custom_merge_tags}' );
return $merge_tags;
}
/**
* Replace custom merge tags in field content if they are found
*
* @param string $text
* @param $form
* @param $entry
*
* @return string
*/
add_filter( 'gform_replace_merge_tags', 'replace_merge_tags', 10, 3 );
function replace_merge_tags( $text, $form, $entry ) {
$tag_merge = '{custom_merge_tags}';
if ( strpos( $text, $tag_merge ) !== false ) {
$text = str_replace( $tag_merge, 'Here it is!', $text );
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment