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.

Embed form on single.php and pre-populating fields

  1. Jan Egbert
    Member

    This is what I already got working after searching this wonderful forum.

    /**
    * Put form with ID 2 in single.php
    */
    add_action( 'hybrid_after_entry', 'email_advertiser', 11);
    function email_advertiser() {
    if ( is_single() && in_category('Marketplace') ) {
    	gravity_form(2, false, false);
    	}
    }
    /**
    * Change notifacation routing in order to attach post author emailadress to the form
    *
    */
    add_filter( 'gform_notification_email_2', 'route_notification', 10, 2 );
    function route_notification($email_to, $entry){
    	global $post;
    	$email_to = get_the_author_email();
    	return $email_to;
    }

    Now what I want is to pre-populate the Subject field with Reaction to + the post title.

    add_filter('gform_field_value_reactionsubject', 'populate_reactionsubject');
    function populate_reactionsubject() {
    global $post;
    return ($post->post_title);
    }

    How do I put 'Reaction to' before post title? I tried to echo it, but that just makes it appear above the form.

    Posted 15 years ago on Wednesday December 16, 2009 | Permalink
  2. Jan Egbert
    Member

    I got it to work.. there is probably some cleaner or easier way, but this seems to work fine.

    add_filter('gform_field_value_reactionsubject', 'populate_reactionsubject');
    function populate_reactionsubject() {
    global $post;
    $reactiontitle = ($post->post_title);
    $reactionsubject = 'Reaction to ' . sprintf( $reactiontitle) . '';
    return $reactionsubject;

    I also added pre-population of the email field.

    add_filter('gform_field_value_reactionemail', 'populate_reactionemail');
    function populate_reactionemail(){
    if ( is_user_logged_in() ) {
     global $userdata;
    	return $userdata->user_email;
    }
    }

    This forum is great for talking to myself without feeling ashamed about it.

    Posted 15 years ago on Wednesday December 16, 2009 | Permalink
  3. LOL Glad you got it figured out Jan. I had forwarded it to Alex to get back with you on, but you figured it out before that happened.

    Posted 15 years ago on Wednesday December 16, 2009 | Permalink
  4. Good stuff Jan, you figured it out.
    You could use the following if you want your code a bit cleaner:

    add_filter('gform_field_value_reactionsubject', 'populate_reactionsubject');
    function populate_reactionsubject() {
    global $post;
    return 'Reaction to ' . $post->post_title;
    }

    Posted 15 years ago on Wednesday December 16, 2009 | Permalink
  5. Jan Egbert
    Member

    Thanks Alex, thanks Carl,

    Gravity Forms opens up a complete new world to me. I'm actually learning PHP. I'm waking up at night, thinking "Hey, I could do that and that... oh and that also".

    For example I use Gravity Forms for a client who thinks WordPress admin is too complex. I did everything to make the interface simpler, but nothing helped. When I introduced a front end login + redirect to a Gravity Forms form, this client was overjoyed. I mean: anyone can fill out a form.

    Posted 15 years ago on Saturday December 19, 2009 | Permalink