我正在将WordPress Gravity Forms与Quiz Addon一起使用,并且我想编辑测验答案的单选输入的HTML。
这是测验答案的每个li
元素当前正在呈现的重力形式:
<li class="gchoice_3_1_1">
<input name="input_1" type="radio" value="gquiz1c4448d0c" checked="checked" id="choice_3_1_1" tabindex="2">
<label for="choice_3_1_1" id="label_3_1_1" class="gquiz-correct-choice">Example label text
<img class="gquiz-indicator" src="https://www.example.com/wp-content/plugins/gravityformsquiz-master/images/tick.png" alt="Correct response">
</label>
</li>
这是我想要实现的(将input
元素移到label
字段中:]
<li class="gchoice_3_1_1">
<label for="choice_3_1_1" id="label_3_1_1" class="gquiz-correct-choice">Example label text
<input name="input_1" type="radio" value="gquiz1c4448d0c" checked="checked" id="choice_3_1_1" tabindex="2">
<img class="gquiz-indicator" src="https://www.example.com/wp-content/plugins/gravityformsquiz-master/images/tick.png" alt="Correct response">
</label>
</li>
这是我在子主题functions.php
中尝试使用的PHP函数,以实现它:
// move radio input inside label
add_filter("gform_field_choices", "radio_input_inside_label", 10, 2);
function radio_input_inside_label($choices, $field){
if($field["type"] != "radio")
return $choices;
$choices = "";
if(is_array($field["choices"])){
$choice_id = 0;
$count = 1;
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : "";
foreach($field["choices"] as $choice){
$id = $field["id"] . '_' . $choice_id++;
$field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"];
$checked = rgar($choice,"isSelected") ? "checked='checked'" : "";
$tabindex = GFCommon::get_tabindex();
$input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id);
$choices .= sprintf("<li class='gchoice_$id'><label for='choice_%s'>%s %s</label></li>", $id, $choice["text"], $input);
$count++;
}
}
return $choices;
}
此功能适用于标准无线电输入,它将input
移到label
内。但不适用于测验无线电输入。
你能看到什么问题吗?
我使用了错误的字段类型选择器。
如果要在正常重力的单选按钮的标签元素内移动单选输入,那么此代码将起作用:
// Gravity Forms Quiz - move radio input inside label
add_filter("gform_field_choices", "radio_input_inside_label", 10, 2);
function radio_input_inside_label($choices, $field){
if($field["type"] != "radio")
return $choices;
$choices = "";
if(is_array($field["choices"])){
$choice_id = 0;
$count = 1;
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : "";
foreach($field["choices"] as $choice){
$id = $field["id"] . '_' . $choice_id++;
$field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"];
$checked = rgar($choice,"isSelected") ? "checked='checked'" : "";
$tabindex = GFCommon::get_tabindex();
$input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id);
$choices .= sprintf("<li class='gchoice_$id'><label for='choice_%s'>%s %s</label></li>", $id, $input, $choice["text"]);
$count++;
}
}
return $choices;
}
但是,如果您安装了Gravity Forms Quiz Addon,并且想在标签内移动THOSE单选输入,则需要此代码。唯一的区别是if($field["type"] != "radio")
被if($field["type"] != "quiz")
替换。
// Gravity Forms Quiz - move radio input inside label
add_filter("gform_field_choices", "radio_input_inside_label", 10, 2);
function radio_input_inside_label($choices, $field){
if($field["type"] != "quiz") // <--- CHANGE HERE
return $choices;
$choices = "";
if(is_array($field["choices"])){
$choice_id = 0;
$count = 1;
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : "";
foreach($field["choices"] as $choice){
$id = $field["id"] . '_' . $choice_id++;
$field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"];
$checked = rgar($choice,"isSelected") ? "checked='checked'" : "";
$tabindex = GFCommon::get_tabindex();
$input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id);
$choices .= sprintf("<li class='gchoice_$id'><label for='choice_%s'>%s %s</label></li>", $id, $input, $choice["text"]);
$count++;
}
}
return $choices;
}