保存Wordpress中多个复选框元变量的数据

时间:2017-02-15 11:57:57

标签: php wordpress post checkbox meta-boxes

我正在学习如何在帖子中添加元数据库。我想创建一组带文本输入和多个checbox的元变量。现在复选框只是放在那里,但最终它们将由来自另一个地方的内容的foreach循环生成,因此对我来说,给它们命名如entry [0],entry [1]等等是很重要的。它们必须通过循环保存,因为我不知道将生成多少。

这是我到目前为止所做的:

 // adding the metaboxes

    function add_post_reference() {
        add_meta_box('post-reference', 'Reference', 'referenceCallBack', 'languagecourses', 'side', 'high');
    }
    add_action('add_meta_boxes', 'add_post_reference');

    // callback function

    function referenceCallBack($post) {
        wp_nonce_field( 'reference_meta_box', 'reference_nonce' );

        $name_value = get_post_meta( $post->ID, '_post_reference_name', true );
        $link_value = get_post_meta( $post->ID, '_post_reference_link', true );

尝试使用我的复选框执行与上面相同的操作,但我不知道该放在那里:

        $teachers_value = get_post_meta( $post->ID, 'what do I put here?', true ); // what do I put here?

现在回应html结构(文本输入工作(值保存),我正在试图弄清楚如何使复选框保存:

        echo '<label for="reference-name">'. 'Reference Name' .'</label>';
        echo '<input type="text" id="reference-name" name="post_reference_name" placeholder="Example" value="'.$name_value.'" size="25"/>';
        echo '<p class="howto">'. 'Add the name of the reference' .'</p>';

        echo '<label for="reference-link">'. 'Reference Link' .'</label>';
        echo '<input type="text" id="reference-link" name="post_reference_link" placeholder="http://www.example.com/" value="'.$link_value.'" size="25"/>';
        echo '<p class="howto">'. 'Add the link of the reference' .'</p>';

        // my checkboxes

        echo '<input type="checkbox" name="entry[0]" value="moredata">';
        echo '<input type="checkbox" name="entry[1]" value="moredata">';
        echo '<input type="checkbox" name="entry[2]" value="moredata">';
        echo '<input type="checkbox" name="entry[3]" value="moredata">';
        echo '<input type="checkbox" name="entry[4]" value="moredata">';



    }

    function save_post_reference( $post_id ) {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }

        if ( ! isset( $_POST['reference_nonce'] ) ) {
            return;
        }
        if ( ! wp_verify_nonce( $_POST['reference_nonce'], 'reference_meta_box' ) ) {
            return;
        }

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

        if ( ! isset( $_POST['post_reference_name'] ) || ! isset( $_POST['post_reference_link'] ) ) {
            return;
        }

        $reference_name = sanitize_text_field( $_POST['post_reference_name'] );
        $reference_link = sanitize_text_field( $_POST['post_reference_link'] );

        // looping through the checkboxes

        for ($i = 0; $i < 5; $i++) {
            $teachers_names = sanitize_text_field($_POST['entry'][$i]);
        }

        update_post_meta( $post_id, '_post_reference_name', $reference_name );
        update_post_meta( $post_id, '_post_reference_link', $reference_link );

再次,我在这里放什么?

        update_post_meta( $post_id, 'whatdoIputhere?', $teachers_names); // what do I put here?

    }

    add_action( 'save_post', 'save_post_reference' );

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

您的HTML代码应该是:

    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';

现在您将保存数据:

key = 'entry';
$values_to_save = array();
$new_values = $_POST['entry'];
$existing_values = get_post_meta( $post_id, $key, true ) ;

if(!empty($existing_values)){
   foreach((array) $existing_values as $existing_value){
      $values_to_save[] = $existing_value;
   }
}


if(!empty($new_values)){
   foreach((array) $new_values as $new_value ){
      $values_to_save[] = $new_value ;
   }
}

update_post_meta( $post_id, $key, $values_to_save ); 

现在要获取数据,请使用以下代码:

$key = 'entry';
$values = get_post_meta( $post_id, $key, true );

foreach((array) $values as $value){
   echo $value . '<br>';
}

答案 1 :(得分:0)

好的,我用你的代码更新了我的函数,这就是它现在的样子:

function add_post_reference() {
    add_meta_box('post-reference', 'Reference', 'referenceCallBack', 'languagecourses', 'side', 'high');
}
add_action('add_meta_boxes', 'add_post_reference');

// callback 

function referenceCallBack($post) {
    wp_nonce_field( 'reference_meta_box', 'reference_nonce' );

    $name_value = get_post_meta( $post->ID, '_post_reference_name', true );
    $link_value = get_post_meta( $post->ID, '_post_reference_link', true );

    $key = 'entry';
    $values = get_post_meta( $post_id, $key, true );

    foreach((array) $values as $value){
        echo $value . '<br>';
    }

    echo '<label for="reference-name">'. 'Reference Name' .'</label>';
    echo '<input type="text" id="reference-name" name="post_reference_name" placeholder="Example" value="'.$name_value.'" size="25"/>';
    echo '<p class="howto">'. 'Add the name of the reference' .'</p>';

    echo '<label for="reference-link">'. 'Reference Link' .'</label>';
    echo '<input type="text" id="reference-link" name="post_reference_link" placeholder="http://www.example.com/" value="'.$link_value.'" size="25"/>';
    echo '<p class="howto">'. 'Add the link of the reference' .'</p>';

    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';
    echo '<input type="checkbox" name="entry[]" value="moredata">';
}

function save_post_reference( $post_id ) {
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    if ( ! isset( $_POST['reference_nonce'] ) ) {
        return;
    }
    if ( ! wp_verify_nonce( $_POST['reference_nonce'], 'reference_meta_box' ) ) {
        return;
    }

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

    if ( ! isset( $_POST['post_reference_name'] ) || ! isset( $_POST['post_reference_link'] ) ) {
        return;
    }

    $reference_name = sanitize_text_field( $_POST['post_reference_name'] );
    $reference_link = sanitize_text_field( $_POST['post_reference_link'] );

    $key = 'entry';
    $values_to_save = array();
    $new_values = $_POST['entry'];
    $existing_values = get_post_meta( $post_id, $key, true ) ;

    if(!empty($existing_values)){
        foreach((array) $existing_values as $existing_value){
            $values_to_save[] = $existing_value;
        }
    }

    if(!empty($new_values)){
        foreach((array) $new_values as $new_value ){
            $values_to_save[] = $new_value ;
        }
    }

    update_post_meta( $post_id, $key, $values_to_save ); 

    update_post_meta( $post_id, '_post_reference_name', $reference_name );
    update_post_meta( $post_id, '_post_reference_link', $reference_link );
}

add_action( 'save_post', 'save_post_reference' );

我改变的一件事是key =$key =,否则会引发错误。

仍然 - 没有变化......我想到的一件事是它可能会保存数据,但复选框仍未选中?