如何在WordPress中保存复选框元框? [英] How to save a checkbox meta box in WordPress?

查看:161
本文介绍了如何在WordPress中保存复选框元框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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.
}

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

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.

任何帮助表示赞赏!

推荐答案

我以前对此有疑问,这是我的解决方法.

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

首先,创建复选框.

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

这篇关于如何在WordPress中保存复选框元框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆