重力形式使用参数动态填充下拉列表

时间:2018-06-18 09:16:36

标签: wordpress gravity-forms-plugin gravityforms

我在wordpress网站上使用Gravity Forms。我想使用动态填充的字段,还需要使用参数自动选择。

我的问题是 我需要从url传递post id。但是当用户提交表单时,需要将帖子标题作为值发送。

例如: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;

}

1 个答案:

答案 0 :(得分:1)

您可以在发送电子邮件时更改字段值,使用gform_notification过滤器即可,请查看以下代码。

    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;
    }

希望这会对你有所帮助。