dongshen6060 2011-09-23 08:42
浏览 44
已采纳

如何在WordPress中保存复选框元框?

I'm trying to add a checkbox into my custom meta box in WordPress and I ran into a problem with saving it - whenever I check the checkbox and update the post/page, it comes back unchecked again.

Here's the code I'm using:

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.
}

This creates the checkbox in the sidebar of the "Edit Page" screen, as it should, no problem there. I'm not sure what should I enter in the value of the checkbox, with text fields it obviously returns whatever was saved as meta information... I tried just using "checked" instead cause that would be my first guess (then simply check for the value when using this meta data), but it didn't save the checkbox either.

Here's the function that saves all the meta data, which I assume causes this problem:

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 );

It works perfectly for text fields, but the checkbox just won't save. I'm not sure if the saving function is wrong, or am I missing something about the value of the checkbox.

Any help appreciated!

  • 写回答

1条回答 默认 最新

  • douzhan8395 2011-09-23 18:04
    关注

    I had trouble with this previously and here is how I solved it.

    First, creating the Checkbox.

    <?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 } ?>
    

    Next, saving.

    <?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;
    } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果