Extra Merge Tags for Gravity Forms
Adds {site_name}, {site_url}, {form_entry_count} and {current_year} merge tags you can use in Gravity Forms notifications and confirmations.
Opens WPIDE on
Code
add_filter( 'gform_custom_merge_tags', function ( $tags ) {
$tags[] = array( 'label' => 'Site name', 'tag' => '{site_name}' );
$tags[] = array( 'label' => 'Site URL', 'tag' => '{site_url}' );
$tags[] = array( 'label' => 'Entries in this form', 'tag' => '{form_entry_count}' );
$tags[] = array( 'label' => 'Current year', 'tag' => '{current_year}' );
return $tags;
} );
add_filter( 'gform_replace_merge_tags', function ( $text, $form, $entry, $url_encode = false, $esc_html = false ) {
if ( false === strpos( (string) $text, '{' ) ) {
return $text;
}
$values = array(
'{site_name}' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
'{site_url}' => home_url( '/' ),
'{current_year}' => wp_date( 'Y' ),
);
if ( false !== strpos( $text, '{form_entry_count}' ) && is_array( $form ) && class_exists( 'GFAPI' ) ) {
$values['{form_entry_count}'] = (string) GFAPI::count_entries( (int) $form['id'], array( 'status' => 'active' ) );
}
foreach ( $values as $tag => $value ) {
if ( $esc_html ) {
$value = esc_html( $value );
}
$text = str_replace( $tag, $url_encode ? rawurlencode( $value ) : $value, $text );
}
return $text;
}, 10, 5 );
Notes
The tags appear in the merge tag menu of notification and confirmation editors.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.