mardi 14 février 2017

Meta boxes - checkboxes and text together

I'm developing a wordpress theme. I've registered two custom post types (teachers and courses) - and to one of them (courses) I added a few metaboxes.

Two of them are text input metaboxes and they work fine (values are saved upon publishing).

Now I would like to add a couple of checkboxes that retrieve the titles and ID's of all the posts from the other custom post type (teachers). The point is to be able to get a teacher (or teachers) associated with a specific course - the user will check the boxes with the names of the teachers that teach a given course.

So far I managed to display the checkboxes, but for now the data isn't saved.

This is how it looks for now:

function wpt_languagecourses_location() {
        global $post;

    // Noncename needed to verify where the data originated
        echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
        wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the location data if its already been entered
        $location = get_post_meta($post->ID, '_location', true);
        $dresscode = get_post_meta($post->ID, '_dresscode', true);



    // Echo out the field
        echo '<p>Course price</p>';
        echo '<input type="text" name="_location" value="' . $location  . '" class="widefat" />';
        echo '<p>How long will the course last?</p>';
        echo '<input type="text" name="_dresscode" value="' . $dresscode  . '" class="widefat" />';?>
        <?php
        $args = array( 'post_type' => 'teachers');

        $loop = new WP_Query( $args );
        ?>
        <p>
            <span class="prfx-row-title">Who is the teacher?</span>
            <div class="prfx-row-content">


                <?php while ( $loop->have_posts() ) : $loop->the_post();?>

                    <label for="meta-checkbox">
                        <input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['meta-checkbox'] ) ) checked( $prfx_stored_meta['meta-checkbox'][0], 'yes' ); ?> />
                        <?php the_title() ?>
                    </label>

                <?php endwhile; ?>
            </div>
        </p>


        <?php }

    // Add the Events Meta Boxes

        function add_languagecourses_metaboxes() {
            add_meta_box('wpt_languagecourses_location', 'Languagecourses Location', 'wpt_languagecourses_location', 'languagecourses', 'side', 'default');
        }


    // Save the Metabox Data

        function wpt_save_languagecourses_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
            if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
                return $post->ID;
            }

    // Is the user allowed to edit the post or page?
            if ( !current_user_can( 'edit_post', $post->ID ))
                return $post->ID;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.

            $languagecourses_meta['_location'] = $_POST['_location'];
            $languagecourses_meta['_dresscode'] = $_POST['_dresscode'];


    // Add values of $events_meta as custom fields

    foreach ($languagecourses_meta as $key => $value) { // Cycle through the $events_meta array!
    if( $post->post_type == 'revision' ) return; // Don't store custom data twice
    $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
        update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn't have a value
    add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

    }
    // Checks for input and saves


    add_action('save_post', 'wpt_save_languagecourses_meta', 1, 2); // save the custom fields

How can I save the data from the checkboxes when user checks them?




Aucun commentaire:

Enregistrer un commentaire