wordpress metabox中的多个复选框

Multiple check boxes in wordpress metabox

如何在 word press meta box 数组中设置多个复选框?

在我的例子中,我的单个复选框工作正常,但我的多个复选框不起作用,只是保存最后一项。

我在保存元框和显示元框时遇到了一些问题。

我不明白如何使用:

get_post_meta()
checked()
update_post_meta()

代码:

        <?php
    class Canco_Meta_Box {

        protected $_meta_box;

        function __construct( $meta_box ) {

            $this->_meta_box = $meta_box;

            add_action( 'admin_menu', array( &$this, 'add_meta_box' ) );
            add_action( 'save_post', array( &$this, 'save_meta_box' ) );

        }

        function add_meta_box() {

            foreach ( $this->_meta_box['pages'] as $page ) {
                add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array( &$this, 'show_meta_box'  ), $page, $this->_meta_box['context'], $this->_meta_box['priority'] );
            }

        }

        function show_meta_box( $post ) {

            echo '<input type="hidden" name="canco_meta_box_nonce" value="', wp_create_nonce( basename( __FILE__ ) ), '" />';

            foreach ( $this->_meta_box['fields'] as $field ) {
                $value = get_post_meta( $post->ID, $field['id'], true );
                $this->meta_box_fields( $field, $value );
            }

        }

        function save_meta_box( $post_id ) {

            global $post;

            if ( ! wp_verify_nonce( $_POST['canco_meta_box_nonce'], basename( __FILE__ ) ) ) {
                return $post_id;
            }

            if ( ! empty( $_POST['canco_page_builder'] ) && $_POST['canco_page_builder'] != "" ) {
                update_post_meta( $post->ID, 'canco_page_builder' , $_POST['canco_page_builder'] );
            } else {
                if ( isset( $post->ID ) ) {
                    delete_post_meta( $post->ID, 'canco_page_builder' );
                }
            }

            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
                return $post_id;
            }

            if ( 'page' == $_POST['post_type'] ) {
                if ( ! current_user_can( 'edit_page', $post_id ) ) {
                    return $post_id;
                }
            } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }

            if ( isset ( $this->_meta_box['fields'] ) ) {
                foreach ( $this->_meta_box['fields'] as $field ) {
                    $old = get_post_meta( $post_id, $field['id'], true );
                    $new = $_POST[$field['id']];
                    if ( $new && $new != $old ) {
                        update_post_meta( $post_id, $field['id'], $new );
                    } elseif ( '' == $new && $old ) {
                        delete_post_meta( $post_id, $field['id'], $old );
                    }
                }
            }
        }

        function meta_box_fields( $field, $value ) {

            echo '<div class="apanel-label">', $field['label'], '</div><div class="apanel-section">';

            switch ( $field['type'] ) {
                case 'checkbox':
                    echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $value ? ' checked="checked"': '', ' />';
                    break;
                case 'multi-checkbox':
                    if ( isset( $field['options'] ) ) {
                        foreach ($field['options'] as $option) {
                            echo '<input type="checkbox" name="', $field['id'],'" value="', $option['value'], '"', in_array( $option['value'], $value ) ? ' checked="checked"': '', ' /><label for="', $option['value'], '">', $option['label'], '</label><br />';
                        }
                    }
                    break;
            }

            echo '</div>';

        }

    }

    $canco_product_details = array(
        'id'        => 'canco_product_details',
        'title'     => 'details',
        'pages'     => array( 'post' ),
        'context'   => 'normal',
        'priority'  => 'high',
        'fields'    => array(
            array(
                'id'      => 'canco_product_color',
                'type'    => 'multi-checkbox',
                'label'   => 'colors',
                'options' => array(
                    array(
                        'value' => '#0038a8',
                        'label' => 'blue',
                    ),
                    array(
                        'value' => '#ee2b2c',
                        'label' => 'red',
                    ),
                    array(
                        'value' => '#139f49',
                        'label' => 'green',
                    ),
                    array(
                        'value' => '#000000',
                        'label' => 'black',
                    ),
                    array(
                        'value' => '#dd1176',
                        'label' => 'pink',
                    ),
                    array(
                        'value' => '#e25f33',
                        'label' => 'orange',
                    ),
                ),
            ),
        ),
    );

    new Canco_Meta_Box( $canco_product_details );

我正在尝试构建一个带有一堆复选框的元框供我的用户选择,我能够让它显示在我的自定义 post 类型编辑屏幕上。但是复选框没有保存...这是构建复选框的代码。

尝试将 [] 添加到您的字段名称中:

echo '<input type="checkbox" name="', $field['id'],'[]" ...

这将保存所有传入值,而不是最后一个。原因是同名的表单元素会互相覆盖:

<input type="checkbox" name="mycheck" value="foo"/>
<input type="checkbox" name="mycheck" value="bar"/>

假设两者都被选中,在后端收到表单时,"mycheck" 将是 bar,但是:

<input type="checkbox" name="mycheck[]" value="foo"/>
<input type="checkbox" name="mycheck[]" value="bar"/>

现在,"mycheck" 的值将是一个包含选中元素值的数组。因此,如果两者都被选中,则该值将是:

Array(
    'foo',
    'bar'
)