I have a custom shortcode that I would like to use for the default value for a field, but I just can’t get it to work.
I’ve defined the shortcode as follows:
add_shortcode('test-name', 'test_shortcode');
function test_shortcode() {
$message = 'hi from the short code';
return $message;
}
This works when I used it in a post as [test-name], but when I use it as the default value specified as “[test-name]” (No quotes) all I see is [test-name] when I view the form, as opposed to ‘hi from the short code’.
Is there something I’m missing about using shortcodes for the default value for fields?
Since you want to use this to set the default value of a field, instead of creating a custom shortcode, you may want to create a custom merge tag using gform_replace_merge_tags filter hook.
You can use the snippet below as a starting point to do what you want, and you would use the {test-name} merge tag to set the default value.
add_filter( 'gform_replace_merge_tags', 'test_merge_tag', 10, 1 );
function test_merge_tag( $text ) {
$message = 'hi from the short code';
$text = str_replace( '{test-name}', $message, $text );
return $text;
}
Thanks! That worked perfectly, I should be able to use merge tags instead of shortcodes for this.
Do you know if shortcodes no longer work for default values? I started with short codes because it’s listed in the help text for the “Allow field to be populated dynamically” checkbox:
Check this option to enable data to be passed to the form and pre-populate this field dynamically. Data can be passed via Query Strings, **Shortcode** and/or Hooks.