dpzo13732 2016-04-19 22:46
浏览 166
已采纳

WordPress:将复选框添加到自定义分类添加/编辑表单

I need to add a list of check-boxes to custom taxonomy add/edit form. I have this code for adding a text field for custom taxonomy form in my plugin and it works fine:

    <?php
    function taxonomy_edit_meta_field($term) {
        $t_id = $term->term_id;
        $term_meta = get_option( "taxonomy_$t_id" ); 
    ?>

        <tr class="form-field">
            <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Term:' ); ?></label></th>
            <td>
                <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
            </td>
        </tr>

    <?php
    }
    add_action( 'product_cat_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2 );

    function save_taxonomy_custom_meta( $term_id ) {
        if ( isset( $_POST['term_meta'] ) ) {
            $t_id = $term_id;
            $term_meta = get_option( "taxonomy_$t_id" );
            $cat_keys = array_keys( $_POST['term_meta'] );
            foreach ( $cat_keys as $key ) {
                if ( isset ( $_POST['term_meta'][$key] ) ) {
                    $term_meta[$key] = $_POST['term_meta'][$key];
                }
            }
            update_option( "taxonomy_$t_id", $term_meta );
        }
    }  
    add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );  
    add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

How I can add a list of check-boxes in the same way?

  • 写回答

2条回答 默认 最新

  • dounai9294 2016-04-20 12:02
    关注

    There were a problem with the saving not checked check-box input. I have fixed it with input type="hidden" with the same value of "name" attribute, as in check-box input.

        <tr class="form-field">
            <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Color:' ); ?></label></th>
            <td>
                <input type="hidden" value="0" name="term_meta[pa_color_attr]">
                <input type="checkbox" <?php echo (!empty($term_meta['pa_color_attr']) ? ' checked="checked" ' : 'test'); ?> value="1" name="term_meta[pa_color_attr]" />
            </td>
        </tr>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 求fpga交通信号灯设计Verilog代码
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联