CMB2 Checkbox Field With True as Default

Image for post
Image for post
CMB2 checkbox field object

The Problem

To your shock and horror.

The Workaround

$cmb2_box_blog->add_field([
'name' => __( 'Disable Blog', 'keystone' ),
'id' => 'cmb2_id_field_disable_blog',
'desc' => 'I\'m a checkbox!',
'type' => 'checkbox',
'sanitization_cb' => 'sanitize_checkbox',
'default' => true, //If it's checked by default
'active_value' => true,
'inactive_value' => false
]);
function sanitize_checkbox($value, $field_args, $field) {
// Return 0 instead of false if null value given.
return is_null($value) ? 0 : $value;
}

The Reason Why

Written by

Front-end Web Developer. Language lover. Traveler. Dude.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store