请求修改WordPress插件PHP代码:将开关输入改为三选项单选框
改造Eventon插件表单:将Yes/No开关改为三色单选框
以下是修改后的完整代码,已经把原有的Yes/No开关替换成了蓝色、红色、黄色三个选项的单选框,适配你的图书馆Eventon插件需求:
// BEGIN EVENTON CODE add_filter('evoau_form_fields', 'evoautest_fields_to_form', 10, 1); function evoautest_fields_to_form($array){ $array['evotest']=array('颜色选择', 'evotest', 'evotest','custom',''); return $array; } // actionUser integration add_action('evoau_frontform_evotest', 'evoautest_fields', 10, 6); // Frontend showing fields and saving values function evoautest_fields($field, $event_id, $default_val, $EPMV, $opt2, $lang){ // 获取已保存的颜色值,默认选蓝色 $saved_color = ($EPMV && !empty($EPMV['evoau_test_value'])) ? $EPMV['evoau_test_value'][0] : 'blue'; echo "<div class='row evotest'><p>"; // 输出字段标签 echo "<label>" . evo_lang_get('evoAULtest_1', '颜色选择', $lang, $opt2) . "</label>"; // 构建单选框组 $colors = array( 'blue' => '蓝色', 'red' => '红色', 'yellow' => '黄色' ); foreach($colors as $value => $label){ $checked = ($saved_color === $value) ? 'checked' : ''; echo "<label style='margin-right: 15px;'> <input type='radio' name='evoau_test_value' value='$value' $checked> $label </label>"; } echo "</p></div>"; } // save the values to event post meta add_action('evoau_save_formfields', 'evoautest_save_values', 10, 3); function evoautest_save_values($field, $fn, $created_event_id){ if( $field =='evotest'){ $color_field = 'evoau_test_value'; if(!empty($_POST[$color_field])){ // 先删除旧值,再添加新值,避免重复 delete_post_meta($created_event_id, $color_field); add_post_meta($created_event_id, $color_field, $_POST[$color_field]); } } } // END EVENTON CODE
关键改动说明:
- 把原表单字段的显示名称改成了「颜色选择」,更贴合需求
- 移除了原有的开关按钮生成方法,手动构建三个颜色单选框,添加间距优化选项布局
- 调整已保存值的读取逻辑,无保存记录时默认选中蓝色
- 保存逻辑增加删除旧元数据的操作,避免同一字段多次保存导致数据混乱
内容的提问来源于stack exchange,提问作者BradG
相关产品推荐
相关产品推荐

