如何使用Gravity Forms复选框URL查询参数传递标签而不是值

时间:2017-09-18 03:50:37

标签: wordpress checkbox gravityforms

我需要将标签列表传递到Wordpress表单中以创建复选框,最好是Gravity Forms。我知道你可以使用:“允许字段动态填充”,但是只填充值而不是同步复选框列表。例如:yahoo.com/?CheckboxLabels=yellow&green&red

复选框:

  1. 黄色
  2. 绿色
  3. 红色
  4. 有没有办法实现这个目标?谢谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

这最终通过添加到主题中的functions.php来为我工作。

<?php
//NOTE: update the ' 1' to the ID of your form
add_filter( 'gform_pre_render_1', 'populate_checkbox' );
add_filter( 'gform_pre_validation_1', 'populate_checkbox' );
add_filter( 'gform_pre_submission_filter_1', 'populate_checkbox' );
add_filter( 'gform_admin_pre_render_1', 'populate_checkbox' );



function populate_checkbox( $form ) {

    foreach( $form['fields'] as &$field )  {

        //NOTE: replace 3 with your checkbox field id
        $field_id = 6;
        if ( $field->id != $field_id ) {
            continue;
        }

        $input_id = 1;


        foreach (explode(',', $_GET["addresses"]) as $name => $value) {

            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            if ( $input_id % 10 == 0 ) {
                $input_id++;
            }

            $choices[] = array( 'text' => $value, 'value' => $value );
            $inputs[] = array( 'label' => $value, 'id' => "{$field_id}.{$input_id}" );

            $input_id++;
        }

        $field->choices = $choices;
        $field->inputs = $inputs;

    }

    return $form;
}

?>