Gravity Custom Merge Tags

I created some custom profile fields following the advice of Thomas Griffin and Stephanie Leary. Like so:

function change_contactmethod( $contactmethods ) {
  // Add some fields
  $contactmethods['twitter'] = 'Twitter Name (no @)';
  $contactmethods['phone'] = 'Phone Number';
  $contactmethods['title'] = 'Title';
  // Remove AIM, Yahoo IM, Google Talk/Jabber
  unset($contactmethods['aim']);
  unset($contactmethods['yim']);
  unset($contactmethods['jabber']);
  // make it go!
  return $contactmethods;
}
add_filter('user_contactmethods','change_contactmethod',10,1);

I want to create a custom merge tag for Gravity Forms to output by user’s phone number ($contactmethods['phone']).

Read More

I can retrieve this info two different ways:

Way 1:

function phone() {
    $userid = get_current_user_id();
    $user_info = get_userdata($userid);
    return get_user_meta($userid, 'phone', true);
}

or Way 2:

function phone() {
    echo the_author_meta('phone', $current_author->ID);
}

I can even create a [phone] shortcode by adding:

add_shortcode('phone', 'phone');

When I try to add my [phone] shortcode as a default input value in Gravity Forms, it does not process the shortcode. Gravity has these things called short tags, which I thought from the documentation on Gravity that {user:[meta_key]} could work as {user:phone}, but I have not set this up as a merge tag yet. I’m not too familiar with JavaScript and what I am doing to set up this merge tag is not working:

add_action("gform_admin_pre_render", "add_merge_tags");
function add_merge_tags($form){
    $mergeTags["phone"] = the_author_meta('phone', $current_author->ID);
?>
<script type="text/javascript">
    gform.addFilter("gform_merge_tags", "add_merge_tags");
    function add_merge_tags(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option){
        mergeTags["phone"].tags.push({ tag: '{phone}', label: 'Phone' });

        return mergeTags;
    }
</script>
<?php
    //return the form object from the php hook  
    return $form;
}

How to create a custom merge tag for an added custom meta field? Or how to allow my field inputs to read my [phone] shortcode?

Related posts

1 comment

  1. You need to add the new merge tags with the gform_custom_merge_tags filter, and then replace them with the gform_replace_merge_tags filter, like this:

    Edit: you need to use the gform_field_content filter to replace the field’s default value, see below.

    add_filter('gform_custom_merge_tags', 'wpse_121476_custom_merge_tags', 10, 4);
    add_filter('gform_replace_merge_tags', 'wpse_121476_replace_merge_tags', 10, 7);
    add_filter('gform_field_content', 'wpse_121476_field_content', 10, 5);
    
    /**
    * add custom merge tags
    * @param array $merge_tags
    * @param int $form_id
    * @param array $fields
    * @param int $element_id
    * @return array
    */
    function wpse_121476_custom_merge_tags($merge_tags, $form_id, $fields, $element_id) {
        $merge_tags[] = array('label' => 'User Phone', 'tag' => '{user_phone}');
    
        return $merge_tags;
    }
    
    /**
    * replace custom merge tags in notifications
    * @param string $text
    * @param array $form
    * @param array $lead
    * @param bool $url_encode
    * @param bool $esc_html
    * @param bool $nl2br
    * @param string $format
    * @return string
    */
    function wpse_121476_replace_merge_tags($text, $form, $lead, $url_encode, $esc_html, $nl2br, $format) {
        $userid = get_current_user_id();
        $phone = $userid ? get_user_meta($userid, 'phone', true) : '';
        $text = str_replace('{user_phone}', $phone, $text);
    
        return $text;
    }
    
    /**
    * replace custom merge tags in field content
    * @param string $field_content
    * @param array $field
    * @param string $value
    * @param int $lead_id
    * @param int $form_id
    * @return string
    */
    function wpse_121476_field_content($field_content, $field, $value, $lead_id, $form_id) {
        if (strpos($field_content, '{user_phone}') !== false) {
            $userid = get_current_user_id();
            $phone = $userid ? get_user_meta($userid, 'phone', true) : '';
            $field_content = str_replace('{user_phone}', $phone, $field_content);
        }
    
        return $field_content;
    }
    

Comments are closed.