phpwordpressgravityforms

How to pass short codes to Gravity Forms email WordPress


I have tried to create short code and use while sending an email from Gravity Forms notification after submission.

HTML in notification

<a href="[site_url]">

But it sends the short code string not value.

I tried this code:

add_action('init', 'wpdocs_add_custom_shortcode');
function wpdocs_add_custom_shortcode()
{
    add_shortcode('site_url', get_site_url());
}

Please help me to get fix this.


Solution

  • In default Gravity Forms or any other plugin depends support on custom shortcodes. To set the some value to the gravity form without creating a custom shortcode, consider using the gform_replace_merge_tags filter hook to create a custom shortcodes.

    Try below snippet

    <?php
    add_filter('gform_replace_merge_tags', 'GF_create_custom_shortcodes', 10, 7);
    function GF_create_custom_shortcodes($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format)
    {
        $custom_merge_tags = array(
            '{site_url}'  => get_site_url(),
        );
        return str_replace(array_keys($custom_merge_tags), array_values($custom_merge_tags), $text);
    }
    

    In other words it will work only for "Gravity Forms" at any place or in any relative hooks / filters created by it.

    Also it will work at the time you using it in Email Notification.