I’m guessing this has been asked before, but I couldn’t find the answer anywhere.
I have a single form that I use in multiple pages/posts with my Call to Action. Because each post targets a different felt need, I’d like to have a different title for each form to override the default form title. I.e., [gravityforms id=‘1’ alternate_title=‘Custom felt need title’].
Is there a way to simply do this? I see the gform_shortcode_ACTION method but not sure if this is the best approach.
Thank you. You said I needed to test the form id. Could I instead simply test for an optional parameter to be passed and if so, replace the title of the current form being called in the short code? Sorry, I’m still very new to Gravity Forms and how their hooks work.
You could test for whatever you like. It would look something like this:
add_filter( 'gform_pre_render', 'set_form_title' );
function set_form_title( $form ) {
// Set form title based on some conditional test
if ( $something == 'whatever' ) {
$form['title'] = "My New Title";
return $form;
}
return $form;
}
However, I haven’t been able to find how to refer to the named parameter and it’s value in the short code via PHP. Everything I’ve found appears to apply to dynamically populating fields which I don’t think is what I need. Any help is appreciated.
Chris, sorry. My intent from the beginning was to try and pass a parameter in the shortcode that would override the default title/label that is displayed. Since I was coming from that angle, I was trying to figure out how to grab the shortcode params to use in your example, but couldn’t get figure out how to grab them. Sorry, if I wasn’t clear about my intent.
Sorry about that. You were clear from the beginning that you wanted to change the form title with the shortcode, using the filter you linked to. I was coming at it from another angle. Let me work on this a little and see if I can get you an example to extend the shortcode to allow for this.
I have not been able to make this work yet using the gform_shortcode_ACTION filter. However, what if you just used title="false" in the shortcode, and then added a custom title to the page where you have embedded the form? Or even just used the page title with the form coming right after? Because you have control over the page content (where the shortcode would be edited) maybe that will work for you?
I will keep looking at this, but I could not find anyone asking for this previously and the last code examples we sent out for this were in 2016 (and only six total between 2013 and today)!
Chris, thank you for continuing to research this. I have used title=‘false’ several times and just added html for the title I wanted displayed. I guess I’ll go back to that. Thank you again for your help.
After working with this a bit more, it became apparent that the https://docs.gravityforms.com/gform_shortcode_form_property/ filter is to extend the [gravityforms] shortcode to do other things. It will not return the form in addition to doing those other things.
No, I definitely meant to include the word not. That shortcode does NOT return a form. In general, it is used to return other information, not return the form.