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 Populate Based on Post Name

  1. Is there a way to dynamically populate a "Single Line Text" field with the name of the post it is inserted into?

    Posted 11 years ago on Saturday December 22, 2012 | Permalink
  2. David Peralty

    There is no easy way to do it. You would have to grab the title using WordPress' built-in functions and using out gform_pre_render push it to the field you want.

    http://www.gravityhelp.com/documentation/page/Gform_pre_render
    http://codex.wordpress.org/Template_Tags/get_the_title

    All my best!

    Posted 11 years ago on Sunday December 23, 2012 | Permalink
  3. Thanks for the head start. Works like a charm and not too much code. Here is a reference for people later. Post in your functions.php file

    add_filter("gform_pre_render", "populate_post_name");
    function populate_post_name($form){
    	global $post;
    	    if($form["id"] != 2) // Change [2] to the ID of the form you want to use
    	       return $form;
    		$post_name = get_the_title($post->ID);
    		$form["fields"][3]["defaultValue"] = $post_name ; //Change [3] to the nth (-1) field you want to populate.
    		return $form;
    
    }
    Posted 11 years ago on Tuesday December 25, 2012 | Permalink
  4. Thanks for sharing your code @leepettijohn.

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink

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