如何在重力表单中自定义自己的选择选项



我在这个自定义重力表单中有两个选择字段,我一直在努力。第一个字段是网络研讨会列表。第二个字段是网络研讨会日期的列表。显示哪些日期取决于您选择的网络研讨会。我计划使用 jQuery 对该列表进行排序,但为此,我需要在网络研讨会日期选择字段中的选择选项中添加一个属性。目前,我不知道如何修改GF的选择选项功能。这是我的自定义函数当前的样子:

//Populate Webinars form field for Webinar dates
add_filter( 'gform_pre_render_2', 'populate_webinar_dates' );
function populate_webinar_dates( $form ) {
    foreach( $form[ 'fields' ] as &$field ) {
        if( $field[ 'type' ] != 'select' || strpos( $field[ 'cssClass' ], 'populate-dates' ) === false )
            continue;
        $posts = new WP_Query( 'numberposts=-1&post_status=publish&post_type=vtl_webinar' );
        $choices = '<option value=" ">Select a date</option>';
        while ( $posts->have_posts() ) : $posts->the_post();
            while ( has_sub_field( 'dates_available' ) ) :
                $post_dates = array( 'date' => get_sub_field( 'date' ) );
                $post_title = array( 'name' => str_replace( " ", "-", get_the_title() ) );
                //$choices[] = array( 'text' => $post_dates['date'], 'value' => $post_dates['date'] );
                $choices = '<option value="' . $post_dates['date'] . '" data-id="' . $post_title . '">' . $post_dates['date'] . '</option>';
            endwhile;                
        endwhile;
        $field[ 'choices' ] = $choices;
    }
    return $form;
}

显然,这一行不起作用:

$choices = '<option value="' . $post_dates['date'] . '" data-id="' . $post_title . '">' . $post_dates['date'] . '</option>';

它期望参数类似于该行上方的注释表单,但您不能只向该行添加自己的属性。您必须根据 GF 正在寻找的一组现有参数来工作。有人可以帮助我解决这个问题吗?我只需要在选项中获取数据属性。

我的做法是

错误的。为了获取我尝试创建的自定义选项,我使用了以下代码而不是上面发布的内容。

add_filter("gform_field_input", "webinar_date_option", 10, 2);
function webinar_date_option($input, $field, $value, $lead_id, $form_id) {
    if ( $field["cssClass"] == "populate-dates" ) {
        $input = '<select name="input_2" id="input_2_2" class="medium gfield_select" tabindex="2">';
        $posts = new WP_Query( 'numberposts=-1&post_status=publish&post_type=vtl_webinar' );
        $input .= '<option value=" ">Select Date</option>';
        while ( $posts->have_posts() ) : $posts->the_post();
            while ( has_sub_field( 'dates_available' ) ) :
                $post_dates = array( 'date' => get_sub_field( 'date' ) );
                $date_id = array( 'id' => get_the_ID() );
                $input .= '<option value="'. $post_dates['date'] .'" data-id="'. $date_id['id'] .'">'. $post_dates['date'] .'</option>';
            endwhile;
        endwhile;
        $input .= '</select>';
    }
    return $input;
}

最新更新