mercredi 15 février 2017

Save data from multiple checkbox metaboxes in Wordpress

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?




Aucun commentaire:

Enregistrer un commentaire