Wordpress自定义帖子类型保存无线电输入元

时间:2015-11-29 15:10:14

标签: php wordpress meta-boxes

我在wordpress插件中创建了一个自定义帖子类型。在自定义帖子类型中,是一些自定义元框 - 在我的自定义元框中,我有一个无线电输入。

我遇到的问题是将此无线电输入的选择保存到数据库中。每当我点击保存时,它都会忘记选择。

这是我的metaboxes的代码:

function render_meta_boxes( $post ) {

    $meta = get_post_custom( $post->ID );
    $name = ! isset( $meta['singer_name'][0] ) ? '' : $meta['singer_name'][0];
    $tagline = ! isset( $meta['singer_tagline'][0] ) ? '' : $meta['singer_tagline'][0];
    $bio = ! isset( $meta['singer_bio'][0] ) ? '' : $meta['singer_bio'][0];
    $gender = ! isset( $meta['singer_gender'][0] ) ? '' : $meta['singer_gender'][0];

    wp_nonce_field( basename( __FILE__ ), 'singer_profile' ); 
?>

    <table class="form-table">

        <tr>
            <td class="singers_meta_box_td" colspan="2">
                <label for="singer_name"><?php _e( 'Singer Name', 'singers_post_type' ); ?>
                </label>
            </td>
            <td colspan="4">
                <input type="text" name="singer_name" class="regular-text" value="<?php echo $name; ?>">
            </td>
        </tr>

        <tr>
            <td class="singers_meta_box_td" colspan="2">
                <label for="singer_tagline"><?php _e( 'Singer Tagline', 'singers_post_type' ); ?>
                </label>
            </td>
            <td colspan="4">
                <input type="text" name="singer_tagline" class="regular-text" value="<?php echo $tagline; ?>">
            </td>
        </tr>

        <tr>
            <td class="singers_meta_box_td" colspan="2">
                <label for="singer_bio"><?php _e( 'Singer Bio', 'singers_post_type' ); ?>
                </label>
            </td>
            <td colspan="4">
                <textarea rows="10" style="width:25em;" name="singer_bio" class="regular-text"><?php echo $bio; ?></textarea>
            </td>
        </tr>

        <tr>
            <td class="singers_meta_box_td" colspan="2">
                <label for="singer_gender"><?php _e( 'Gender', 'singers_post_type' ); ?>
                </label>
            </td>
            <td colspan="4">
                <input type="radio" name="singer_gender" value="male"> Male<br>
                <input type="radio" name="singer_gender" value="female"> Female
            </td>
        </tr>

    </table>

<?php }

/**
* Save metaboxes
*
* @since 0.1.0
*/
function save_meta_boxes( $post_id ) {

    global $post;

    // Verify nonce
    if ( !isset( $_POST['singer_profile'] ) || !wp_verify_nonce( $_POST['singer_profile'], basename(__FILE__) ) ) {
        return $post_id;
    }

    // Check Autosave
    if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || ( defined('DOING_AJAX') && DOING_AJAX) || isset($_REQUEST['bulk_edit']) ) {
        return $post_id;
    }

    // Don't save if only a revision
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }

    // Check permissions
    if ( !current_user_can( 'edit_post', $post->ID ) ) {
        return $post_id;
    }

    $meta['singer_name'] = ( isset( $_POST['singer_name'] ) ? esc_textarea( $_POST['singer_name'] ) : '' );

    $meta['singer_bio'] = ( isset( $_POST['singer_bio'] ) ? esc_textarea( $_POST['singer_bio'] ) : '' );

    $meta['singer_tagline'] = ( isset( $_POST['singer_tagline'] ) ? esc_textarea( $_POST['singer_tagline'] ) : '' );

    $meta['singer_gender'] = ( isset( $_POST['singer_gender'] ) ? sanitize_html_class( $_POST['singer_gender'] ) : '' );

    foreach ( $meta as $key => $value ) {
        update_post_meta( $post->ID, $key, $value );
    }
}

您可以看到无线电输入只是男性和女性选项,然后在脚本的底部是处理保存的位置。我无法弄清楚如何做出选择保存。

1 个答案:

答案 0 :(得分:0)

好的,首先你没有添加

add_action('save_post', 'save_meta_boxes');

但是我假设你有这个,因为应该保存其他选项(通过你的帐户)。

接下来,我将这些变量写在元数据箱中,如下所示:

$meta = get_post_custom( $post->ID );
$name = (isset($meta['singer_name'][0]) && $meta['singer_name'][0] != '') ? $meta['singer_name'][0] : '';
$tagline = (isset($meta['singer_tagline'][0]) && $meta['singer_tagline'][0] != '') ? $meta['singer_tagline'][0] : '';
$bio = (isset($meta['singer_bio'][0]) && $meta['singer_bio'][0] != '') ? $meta['singer_bio'][0] : '';
$gender = (isset($meta['singer_gender'][0]) && $meta['singer_gender'][0] != '') ? $meta['singer_gender'][0] : '';

我发现它更加万无一失。

您需要的是从数据库中提取的值,以查看是否选中/选中了您的无线电选项(以及复选框的相同内容)。因此,在render_meta_boxes()函数内,在帖子自定义变量之后添加:

$value = get_post_meta( $post->ID, 'gender-select', true );

现在您的无线电输入必须看起来像

<td colspan="4">
    <input type="radio" name="singer_gender" value="male" <?php checked( $value, 'male' ); ?>> Male<br>
    <input type="radio" name="singer_gender" value="female" <?php checked( $value, 'female' ); ?>> Female
</td>

在这里,您要检查邮政元组中的值是男性还是女性,并根据您检查该单选按钮。

最后但并非最不重要,您的保存功能应该以{{1​​}}作为参数,而不是$post

$post_id

您应该从保存框中删除function save_meta_boxes( $post ) { global $post; $post_id = $post->ID; ... } ,因为它什么都不做。而是使用:

$meta['singer_gender']

您的$gender_meta_value = ( isset( $_POST['singer_gender'] ) ? sanitize_html_class( $_POST['singer_gender'] ) : '' ); update_post_meta( $post_id, 'gender-select', $gender_meta_value ); 是后置元数据中的值,稍后会在您gender-select变量中进行检查。

那就是