在Metabox中将复选框设为默认选中

时间:2017-12-18 作者:tsquez

好吧,我又来了。。哈哈,对不起。是的,我到处都在寻找答案,但是我使用的代码不同,我无法理解它。

所以我从GenerateWP.com 我再次请求帮助,但始终没有得到答复。这是:

class TP_Primo_Featured_Image_Options_Custom_Meta_Box {
    public function __construct() {
        if ( is_admin() ) {
            add_action(\'load-post.php\',array($this,\'init_metabox\'));
            add_action(\'load-post-new.php\',array($this,\'init_metabox\'));
        }
    }

    public function init_metabox() {
        add_action(\'add_meta_boxes\', array($this,\'add_metabox\'));
        add_action(\'save_post\', array($this,\'save_metabox\'), 10, 2 );
    }

    public function add_metabox() {
        add_meta_box(
            \'tp-primo-featured-image-options\',
            __( \'Featured Image Options\', \'tp-primo\' ),
            array( $this, \'render_metaboxrender_tp_primo_featured_image_options_metabox\' ),
            array( \'post\', \'page\' ),
            \'side\',
            \'default\'
        );
    }

    public function render_metaboxrender_tp_primo_featured_image_options_metabox( $post ) {

        // Retrieve an existing value from the database.
        $tp_primo_show_below_title = get_post_meta($post->ID,\'tp_primo_show_below_title\',true);
        $tp_primo_show_above_title = get_post_meta($post->ID,\'tp_primo_show_above_title\',true);
        $tp_primo_set_page_header = get_post_meta($post->ID,\'tp_primo_set_page_header\',true);
        $tp_primo_page_header_title_overlay = get_post_meta($post->ID, \'tp_primo_page_header_title_overlay\',true);
        $tp_primo_page_header_title_overlay_parallax = get_post_meta($post->ID,\'tp_primo_page_header_title_overlay_parallax\',true);
        $tp_primo_page_header_height = get_post_meta($post->ID, \'tp_primo_page_header_height\', true );

        // Set default values.
        if( empty( $tp_primo_show_below_title ) ) $tp_primo_show_below_title = \'checked\';
        if( empty( $tp_primo_page_header_height ) ) $tp_primo_page_header_height = \'\';

        // Form fields.
        echo \'<p><label><input type="checkbox" name="tp_primo_show_below_title" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_show_below_title . \'" \' . checked( $tp_primo_show_below_title, \'checked\', false ) . \'> \' . __( \'Show Below Title\', \'tp-primo\' ) . \'</label></p>\';
        echo \'<p><label><input type="checkbox" name="tp_primo_show_above_title" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_show_above_title . \'" \' . checked( $tp_primo_show_above_title, \'checked\', false ) . \'> \' . __( \'Show Above Title\', \'tp-primo\' ) . \'</label></p>\';
        echo \'<p><label><input type="checkbox" name="tp_primo_set_page_header" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_set_page_header . \'" \' . checked( $tp_primo_set_page_header, \'checked\', false ) . \'> \' . __( \'Set as Page Header\', \'tp-primo\' ) . \'</label></p>\';
        echo \'<p><label><input type="checkbox" name="tp_primo_page_header_title_overlay" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_page_header_title_overlay . \'" \' . checked( $tp_primo_page_header_title_overlay, \'checked\', false ) . \'> \' . __( \'Page Header: Title Overlay\', \'tp-primo\' ) . \'</label></p>\';
        echo \'<p><label><input type="checkbox" name="tp_primo_page_header_title_overlay_parallax" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_page_header_title_overlay_parallax . \'" \' . checked( $tp_primo_page_header_title_overlay_parallax, \'checked\', false ) . \'> \' . __( \'Page Header: Title Overlay/Parrallax\', \'tp-primo\' ) . \'</label></p>\';
        echo \'<label for="tp_primo_page_header_height" class="tp_primo_page_header_height_label">\' . __(\'Page Header Height\', \'tp-primo\' ) . \'</label>\';
        echo \'<input type="text" id="tp_primo_page_header_height" name="tp_primo_page_header_height" class="tp_primo_page_header_height_field" placeholder="\' . esc_attr__( \'\', \'tp-primo\' ) . \'" value="\' . esc_attr( $tp_primo_page_header_height ) . \'">\';
        echo \'<p class="fi description">\' . __( \'Use PX, %, REM, EM and VH based values.\', \'tp-primo\' ) . \'</p>\';
    }

    public function save_metabox( $post_id, $post ) {

        // Sanitize user input.
        $tp_primo_new_show_below_title = isset($_POST[\'tp_primo_show_below_title\']) ? \'checked\' : \'\';
        $tp_primo_new_show_above_title = isset($_POST[\'tp_primo_show_above_title\']) ? \'checked\' : \'\';
        $tp_primo_new_set_page_header = isset($_POST[\'tp_primo_set_page_header\']) ? \'checked\' : \'\';
        $tp_primo_new_page_header_title_overlay = isset($_POST[\'tp_primo_page_header_title_overlay\']) ? \'checked\' : \'\';
        $tp_primo_new_page_header_title_overlay_parallax = isset($_POST[ \'tp_primo_page_header_title_overlay_parallax\']) ? \'checked\' : \'\';
        $tp_primo_new_page_header_height = isset($_POST[\'tp_primo_page_header_height\' ] ) ? sanitize_text_field( $_POST[ \'tp_primo_page_header_height\' ] ) : \'\';

        // Update the meta field in the database.
        update_post_meta($post_id,\'tp_primo_show_below_title\',$tp_primo_new_show_below_title);
        update_post_meta($post_id,\'tp_primo_show_above_title\',$tp_primo_new_show_above_title);
        update_post_meta($post_id,\'tp_primo_set_page_header\',$tp_primo_new_set_page_header);
        update_post_meta($post_id,\'tp_primo_page_header_title_overlay\',$tp_primo_new_page_header_title_overlay);
        update_post_meta($post_id,\'tp_primo_page_header_title_overlay_parallax\',$tp_primo_new_page_header_title_overlay_parallax);
        update_post_meta( $post_id,\'tp_primo_page_header_height\',$tp_primo_new_page_header_height);
    }
}
new TP_Primo_Featured_Image_Options_Custom_Meta_Box;
如你所见,我尝试添加if( empty( $tp_primo_show_below_title ) ) $tp_primo_show_below_title = \'checked\'; 在下面的部分中// Set default values.

"Show Below Title" 默认选中(这是我想要的…呜呜),但如果我取消选中并选中"Show Above Title" 点击显示的更新"Show Below Title" AND "Show Above Title" 已选中。。。呜呜!

我需要设置“在标题下显示”($tp_primo_show_below_title) 作为默认值。我似乎无法理解。我错过了什么?

非常感谢您的帮助。

1 个回复
SO网友:inarilo

您正在将未选中的复选框值保存为空字符串:

public function save_metabox( $post_id, $post ) {

    // Sanitize user input.
    $tp_primo_new_show_below_title = isset($_POST[\'tp_primo_show_below_title\']) ? \'checked\' : \'\';
    ...

}
因此,当您获取保存的未选中值,然后检查if(empty(...)), 它返回true并设置\'checked\' 作为默认值,因为empty(...):

如果var存在并且具有非空、非零值,则返回FALSE。否则返回TRUE。

以下内容视为空:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
get_post_meta 如果未找到值,则返回空字符串。唯一的区别方法是为未选中的框保存单独的值。因此,与其保存\'checked\'\'\' 作为元值,最好使用10. 这也会减少数据库中的空间。

编辑:

更改功能如下:

public function render_metaboxrender_tp_primo_featured_image_options_metabox( $post ) {

    // Retrieve an existing value from the database.

    ...

    // Set default values.
    // Only non-existent checkbox values will be empty strings, as we are saving \'1\'s and \'0\'s.
    // We cannot use the empty function since empty(\'0\') will also return true.
    if( $tp_primo_show_below_title == \'\' ) $tp_primo_show_below_title = \'1\'; //checked by default
    if( $tp_primo_page_header_height == \'\' ) $tp_primo_page_header_height = \'0\'; //unchecked by default

    // Form fields.
    echo \'<p><label><input type="checkbox" name="tp_primo_show_below_title" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_show_below_title . \'" \' . checked( $tp_primo_show_below_title, \'1\', false ) . \'> \' . __( \'Show Below Title\', \'tp-primo\' ) . \'</label></p>\';
    echo \'<p><label><input type="checkbox" name="tp_primo_show_above_title" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_show_above_title . \'" \' . checked( $tp_primo_show_above_title, \'1\', false ) . \'> \' . __( \'Show Above Title\', \'tp-primo\' ) . \'</label></p>\';
    echo \'<p><label><input type="checkbox" name="tp_primo_set_page_header" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_set_page_header . \'" \' . checked( $tp_primo_set_page_header, \'1\', false ) . \'> \' . __( \'Set as Page Header\', \'tp-primo\' ) . \'</label></p>\';
    echo \'<p><label><input type="checkbox" name="tp_primo_page_header_title_overlay" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_page_header_title_overlay . \'" \' . checked( $tp_primo_page_header_title_overlay, \'1\', false ) . \'> \' . __( \'Page Header: Title Overlay\', \'tp-primo\' ) . \'</label></p>\';
    echo \'<p><label><input type="checkbox" name="tp_primo_page_header_title_overlay_parallax" class="tp_primo_featured_image_locations_field" value="\' . $tp_primo_page_header_title_overlay_parallax . \'" \' . checked( $tp_primo_page_header_title_overlay_parallax, \'1\', false ) . \'> \' . __( \'Page Header: Title Overlay/Parrallax\', \'tp-primo\' ) . \'</label></p>\';

    ...

}

public function save_metabox( $post_id, $post ) {

    // Sanitize user input.
    $tp_primo_new_show_below_title = isset($_POST[\'tp_primo_show_below_title\']) ? \'1\' : \'0\';
    $tp_primo_new_show_above_title = isset($_POST[\'tp_primo_show_above_title\']) ? \'1\' : \'0\';
    $tp_primo_new_set_page_header = isset($_POST[\'tp_primo_set_page_header\']) ? \'1\' : \'0\';
    $tp_primo_new_page_header_title_overlay = isset($_POST[\'tp_primo_page_header_title_overlay\']) ? \'1\' : \'0\';
    $tp_primo_new_page_header_title_overlay_parallax = isset($_POST[ \'tp_primo_page_header_title_overlay_parallax\']) ? \'1\' : \'0\';

    ...

    // Update the meta field in the database.

    ...

}

结束

相关推荐

Metabox-显示Metabox的值

这里是新手。我挣扎了几个小时。我正在开发我的第一个WP插件,在那里我可以输入产品(产品标题和描述)。wp似乎将我的描述保存在db中,因为如果我在后端打开产品,我可以看到我保存的描述。但如果我试图回应描述,我会得到一个空值。以下是一些代码片段:代谢箱:function product_custom_meta(){ add_meta_box(\'product_meta\', __(\'Product Title\', \'product-textdomain\' ), \'product_m