Shortcode for default value [RESOLVED]

Hi,

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?

Hi Mark,

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;
}

Best,

Hi Samuel,

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.

Thanks

Hi Mark. In that text you highlighted, the Shortcode referred to there is the gravityforms shortcode, passing in a default value like this:

[gravityform id="1" field_values="parameter_name1=value1&parameter_name2=value2"]

Documented in that page:

It was never possible to use shortcodes as default values for fields in the form builder, but custom merge tags will work.

1 Like

Ah, I see, sorry about that I misunderstood what would work based on the help text. Thanks for the conformation.

1 Like