元框 - 复选框和文本

时间:2017-02-15 02:36:55

标签: php wordpress checkbox meta-boxes

我正在开发一个wordpress主题。我已经注册了两种自定义帖子类型(教师和课程) - 其中一种(课程)我添加了一些元文件。

其中两个是文本输入元变量,它们工作正常(值在发布时保存)。

现在我想添加一些复选框,用于检索其他自定义帖子类型(教师)中所有帖子的标题和ID。关键是能够让一个与特定课程相关的教师(或教师) - 用户将使用教授特定课程的教师的名字来检查框。

到目前为止,我设法显示了复选框,但目前尚未保存数据。

这就是它现在的样子:

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

如何在用户检查时从复选框中保存数据?

1 个答案:

答案 0 :(得分:0)

我会使用一些jQuery。因此,在检查时会分配一个name属性,并在我们检查时分配一个新值。

<input type="checkbox" name="meta_checkbox_checked[]" /> // checked
<input type="checkbox" name="meta_checkbox_notchecked[]" /> // not checked

然后保存

if($_POST['meta_checkbox_checked']) {
    $i = 0;
    foreach($_POST['meta_checkbox_checked']) {
        $metabox[$i] = $_POST['varient_data'][$i];
        $i++;
    }
    update_post_meta($post_id, "meta_checkbox_checked", $metabox);
}

如果您需要jQuery代码,请告诉我。我没有测试过这段代码,请告诉我。