1

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']).

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?

closed as off-topic by Johannes Pille, birgire, TheDeadMedic, mrwweb, Mark Kaplun Aug 30 '14 at 2:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Johannes Pille, birgire, TheDeadMedic, mrwweb, Mark Kaplun
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Seems to be some information lacking here. I assume you are using Gravity Forms User Registration add-on? What exactly do you need a shortcode for? Can't you just map a specific phone field to the user_meta? If that's not available in the user registration settings for the form, you should easily be able to do this with a hook. – GhostToast Nov 7 '13 at 21:42
  • @GhostToast You are correct the original purpose was for user registration, but I created another form for users to fill in their time sheet. I wanted the field to autofill using either a shorcode (inputs done do do_shortcode) or merge tag which I'm stumbling over how to do. – Rizzo Nov 8 '13 at 13:51
  • Why a shortcode? You're trying to embed the same form on many pages, and then want some magic shortcode to fill stuff in based on what page it's on? If that is your goal, I'd opt for post_meta or something rather than shortcode. Or are you trying to stuff extra params into the gform embed code? I think what you are trying to do probably has simpler methods. Can you add to the question a more in-depth explanation as to your goals? – GhostToast Nov 8 '13 at 14:54
5

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;
}
  • I was able to use this to create the custom merge tag, but the "gform_replace_merge_tags" never worked. Is it better to use $phone = get_user_meta($user_id, 'phone', true); or $phone = the_author_meta('phone', $current_author->ID); – Rizzo Nov 11 '13 at 14:00
  • also what is the significance of the 10, 4 and 10, 7 at the end of each filter? – Rizzo Nov 11 '13 at 14:01
  • oh almost forgot to notify @webaware – Rizzo Nov 11 '13 at 19:35
  • I've updated my answer, it should now affect the default values (like you asked!) as well as form notifications -- sorry about that! Regarding the 10,4 and 10,7 please refer to codex and read about priority and accepted_args. – webaware Nov 12 '13 at 0:52
  • that worked perfect!! – Rizzo Nov 12 '13 at 13:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.