【发布时间】:2020-03-25 14:38:53
【问题描述】:
我正在使用带有一个单选按钮和一个复选框字段的重力表单,它们都有相同的选择。 我需要的是当我的用户在单选按钮上选择一个选项时,该选项会自动在复选框字段中预先选择。 他不必再次选择它。
我看到我必须使用一些过滤器,例如下面的那个,但我无法成功自定义它以完成我需要的操作。
如果有人可以帮助我,他会救我的命。
<?php
# Make sure to replace {id} with your form's id
add_filter( 'gform_pre_render_{id}', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
/**
* Loop through form fields
*
* Note we are using the `$field` array as a direct reference using `&`.
* This means that changing its value will within the loop will
* change the corresponding `$form` array item
*/
foreach( $form['fields'] as &$field ) {
# Make sure to change `1` to the ID of the checkbox field that you want to pre-populate
if( 1 === $field->id ) {
/**
* Loop through the choices for this checkbox
*
* Note again that we are passing `$choice` by reference in order to change the
* corresponding array item within the `$field` array
*/
foreach( $field->choices as &$choice ) {
/**
* If this choice has a value of 'red' or 'blue', then make sure the checkbox is pre-
checked
* by setting the `isSelected` parameter to `true`
*/
if( 'red' === $choice['value'] || 'blue' === $choice['value'] ) {
$choice['isSelected'] = true;
}
} # end foreach: $field->choices
} # end if: $field->id equals 1
} # end foreach: $form['fields']
# return the altered `$form` array to Gravity Forms
return $form;
} # end: my_populate_checkbox
【问题讨论】:
标签: wordpress select checkbox gravityforms