1

I'm learning how to add metaboxes to posts. I would like to create a group of metaboxes with text inputs and multiple checboxes. For now the checkboxes are just put there like that, but eventually they will be generated by a foreach loop with content from another place, so it's important for me to give them names like entry[0], entry[1] and so on. They have to be saved by a loop as I will not know how many will be generated.

This is what I have so far:

 // 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 );

trying to do the same as above with my checkboxes but I don't know what to put there:

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

Echoing the html structure now (the text inputs work (values get saved), I'm trying to figure out how to make the checkboxes save as well:

        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 );

Again, what do I put here?

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

    }

    add_action( 'save_post', 'save_post_reference' );

Could anybody please help me on that?

oneday
  • 1,599
  • 5
  • 18
  • 29
  • simple your meta key..... use `get_post_meta($post_ID, 'your key', true);` to fetch the data and `update_post_meta( $post_id, 'your key', $teachers_names);` to save it – Happy Coding Feb 15 '17 at 12:04
  • I'm trying to figure out what exactly is the meta key, for the first two I see they use the name of the input field like this: `update_post_meta( $post_id, '_post_reference_name', $reference_name ); update_post_meta( $post_id, '_post_reference_link', $reference_link );` So I put 'entry[]' here, but it doesn't save. `update_post_meta( $post_id, 'entry[]', $teachers_names); ` Could you please shed some more light here? – oneday Feb 15 '17 at 12:20
  • `update_post_meta( $post_id, 'entry', $teachers_names)` this is sufficient – Happy Coding Feb 15 '17 at 12:23
  • Thanks, but unfortunately still not saving. – oneday Feb 15 '17 at 12:27

2 Answers2

0

Your HTML code should be like :

    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">';

now you will save the data :

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 ); 

Now to fetch the data use the below code :

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

foreach((array) $values as $value){
   echo $value . '<br>';
}
Happy Coding
  • 2,517
  • 1
  • 13
  • 24
  • Trying to make it work, one question - shouldn't it be `$key = 'entry';` instead of `key = 'entry';` ? – oneday Feb 15 '17 at 12:45
  • I get this warning now: Warning: Invalid argument supplied for foreach() in [...] I read here that this happens when you supply the foreach with data that is not an array: http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach – oneday Feb 15 '17 at 12:51
0

Ok, I updated my functions with your code and this is how it looks now:

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' );

One thing I change was that key = to $key = as otherwise it was throwing an error.

And still - no change... one thing I thought about is that maybe it does save the data, but the checkboxes remain unchecked?

oneday
  • 1,599
  • 5
  • 18
  • 29