【问题标题】:How to save a checkbox meta box in WordPress?如何在 WordPress 中保存复选框元框?
【发布时间】:2011-11-23 11:48:35
【问题描述】:

我正在尝试在 WordPress 的自定义元框中添加一个复选框,但在保存时遇到了问题 - 每当我选中该复选框并更新帖子/页面时,它都会再次未选中。

这是我正在使用的代码:

add_meta_box(
    'sl-meta-box-sidebar',      // id
    'Sidebar On/Off',           // title
    'sl_meta_box_sidebar',      // callback function
    'page',                     // type of write screen
    'side',                     // context
    'low'                       // priority
);

function sl_meta_box_sidebar() {
    global $meta; sl_post_meta( $post->ID ); ?>
    <input type="checkbox" name="sl_meta[sidebar]" value="<?php echo htmlspecialchars ($meta['sidebar']); ?>" />Check to turn the sidebar <strong>off</strong> on this page.
}

这会在“编辑页面”屏幕的侧边栏中创建复选框,应该没有问题。我不确定我应该在复选框的值中输入什么,对于文本字段,它显然会返回保存为元信息的任何内容......我尝试只使用“checked”而不是因为这将是我的第一个猜测(然后只需检查使用此元数据时的值),但它也没有保存复选框。

这是保存所有元数据的函数,我认为这会导致此问题:

function sl_save_meta_box( $post_id, $post ) {
    global $post, $type;

    $post = get_post( $post_id );

    if( !isset( $_POST[ "sl_meta" ] ) )
        return;

    if( $post->post_type == 'revision' )
        return;

    if( !current_user_can( 'edit_post', $post_id ))
        return; 

    $meta = apply_filters( 'sl_post_meta', $_POST[ "sl_meta" ] );

    foreach( $meta as $key => $meta_box ) {
        $key = 'meta_' . $key;
        $curdata = $meta_box;
        $olddata = get_post_meta( $post_id, $key, true );

        if( $olddata == "" && $curdata != "" )
            add_post_meta( $post_id, $key, $curdata );
        elseif( $curdata != $olddata )
            update_post_meta( $post_id, $key, $curdata, $olddata );
        elseif( $curdata == "" )
            delete_post_meta( $post_id, $key );
    }

    do_action( 'sl_saved_meta', $post );
}

add_action( 'save_post', 'sl_save_meta_box', 1, 2 );

它非常适用于文本字段,但复选框不会保存。我不确定保存功能是否错误,或者我是否遗漏了复选框的值。

任何帮助表示赞赏!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我之前遇到过这个问题,我是这样解决的。

    首先,创建复选框。

    <?php
    function sl_meta_box_sidebar(){
        global $post;
        $custom = get_post_custom($post->ID);
        $sl_meta_box_sidebar = $custom["sl-meta-box-sidebar"][0]; 
    ?>
    
    <input type="checkbox" name="sl-meta-box-sidebar" <?php if( $sl_meta_box_sidebar == true ) { ?>checked="checked"<?php } ?> />  Check the Box.
    <?php } ?>
    

    接下来,保存。

    <?php
    add_action('save_post', 'save_details');
    
    function save_details($post_ID = 0) {
        $post_ID = (int) $post_ID;
        $post_type = get_post_type( $post_ID );
        $post_status = get_post_status( $post_ID );
    
        if ($post_type) {
        update_post_meta($post_ID, "sl-meta-box-sidebar", $_POST["sl-meta-box-sidebar"]);
        }
       return $post_ID;
    } ?>
    

    【讨论】:

    • 注意 -- 如果您计划在分类中使用复选框,则需要将复选框保存在 term_relationships 表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多