PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamically add Wordpress page title to Gravity Form field.

  1. I have a Gravity Form on a wordpress page. I have a field called product. The product is the name of the wordpress page. How can I dynamically add the page name to the product field on the gravity form. I have very limited knowledge of using Gravity Forms.

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  2. David Peralty

    What is your PHP coding ability? You could use a Gravity Form hook to grab the current page name from WordPress, and push it into the field you want.

    Have a look at the following hooks:
    http://www.gravityhelp.com/documentation/page/Gform_pre_render
    http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name

    And here are details on where code goes when extending Gravity Forms:
    http://www.gravityhelp.com/documentation/page/Where_Do_I_Put_This_Code%3F

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  3. Hi David,
    I do not have any PHP coding ability! I am OK at following instructions and I am a quick learner but other than that I have no idea!

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  4. OK, so I see that I need to add some PHP code to the functions.php page of my theme. Is there an example of PHP code I need to add to add the page title to a form field?

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  5. OK,

    So I have added the value 'product' to the parameter name in the form field advanced tab. I have to add some PHP code similar to the code below to the functions.php page of the theme.

    How do I change the code below to pass the page title to the field with the parameter name 'product'?

    <?php
    add_filter("gform_field_value_date", "populate_date");
    function populate_date($value){
    return "10/10/2010";
    }
    ?>

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  6. OK, I am making progress, I have added the code below to the functions.php page and now the date appears in the product field. However, I want the page title to appear in the product field. I therefore need to change the return value from the date to something that will show the page title. What do I add?

    <?php
    add_filter("gform_field_value_product", "populate_product");
    function populate_product($value){
    return "10/10/2010";
    }
    ?>
    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  7. David Peralty

    Here is what I used:

    add_filter("gform_field_value_title", "page_title");
    
    function page_title($form){
    $currenttitle = get_the_title();
    return $currenttitle;
    }
    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  8. David Peralty

    And as a side note, I'm glad to see you are experimenting. :)

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  9. I'm giving it a go!

    I added your code to the functions.php page but nothing happened. Do I have to combine your code with mine. For example, using your code, where does it state that the page title will go into the product field on the gravity form?

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  10. David Peralty

    Really what you want from me is the $currenttitle = get_the_title(); line and then returning that value to your form field.

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  11. Yes!

    THis code worked!

    <?php
    
    add_filter("gform_field_value_product", "page_title");
    
    function page_title($form){
    $currenttitle = get_the_title();
    return $currenttitle;
    }

    Wow, my first bit of PHP! Thanks a lot David.

    I hope other people can learn from this also!

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink
  12. David Peralty

    I hope so too. :) Glad to hear it worked.

    Posted 12 years ago on Tuesday June 26, 2012 | Permalink

This topic has been resolved and has been closed to new replies.