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?
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.