【问题标题】:How do I customize my own select options in Gravity Forms?如何在 Gravity Forms 中自定义我自己的选择选项?
【发布时间】:2015-01-06 21:42:19
【问题描述】:

我一直在努力解决这个自定义重力表单中的两个选择字段。第一个字段是网络研讨会列表。第二个字段是网络研讨会日期列表。显示哪些日期取决于您选择的网络研讨会。我计划使用 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 正在寻找的现有参数集进行工作。有人可以帮我解决这个问题吗?我只需要在选项中获取数据属性即可。

【问题讨论】:

    标签: wordpress forms gravity-forms-plugin


    【解决方案1】:

    我做错了。为了获得我试图创建的自定义选项,我使用了以下代码而不是上面发布的代码。

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

    【讨论】:

      猜你喜欢
      • 2017-04-06
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      相关资源
      最近更新 更多