0

I'm using Gravity Forms on my wordpress website. I wanted to use dynamically populated fields and also need to auto selected with parameter.

My problem is I need to pass the post id from url. but when user submit the form , need to send the post title as the value.

Eg : myurl/?staff_member=14

add_filter( 'gform_pre_render_2', 'populate_posts' );
add_filter( 'gform_pre_validation_2', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_2', 'populate_posts' );
add_filter( 'gform_admin_pre_render_2', 'populate_posts' );


function populate_posts( $form ){

  foreach ( $form['fields'] as &$field ) {
    if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
      continue;
    }

    $posts = get_posts(array(
      'numberposts' => -1,
      'post_type'     => 'staff',
    ));

    $choices = array();

    foreach ( $posts as $post ) {
      $choices[] = array( 
        'text'  => $post->post_title, 
        'value' => $post->ID, //** <- I am using post ID , because url parameter need to be the post id. also i need to send post title when submit the form
      );
    }

    $field->placeholder = 'Select Staff Member';
    $field->choices = $choices;
  }

  return $form;

}
2
  • So here you want post tile in email that is the only thing right ? Commented Jun 18, 2018 at 9:25
  • @dipmala Yes, Thank you very much. Commented Jun 18, 2018 at 9:56

1 Answer 1

1

You can change field value while sending the email , that can be possible using gform_notification filter, kindly check below code for the same.

    add_filter( 'gform_notification_1', 'change_notification_email', 10, 3 ); // here 1 is my form id, change with your form id
    function change_notification_email( $notification, $form, $entry ) {

        if ( $notification['name'] == 'Admin Notification' ) 
        { 
            $fieldid=2;
            $postid=$entry[$fieldid]; // here 2 is my field id change with your field id
            $entry[$fieldid] =get_the_title($postid); // this will change the field value in email.
        }

        return $notification;
    }

Hope this will help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I will try this. Appreciate your answer,
This is works for me. Thank you very much !!! Really appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.