Browse files

Fixed logic once more, added filter for checkbox validation

No really, I've got it working this time. Now you can set a default
"checked" status, and you can accurately test whether it's on or off
after that.
  • Loading branch information...
1 parent 03d04ea commit e84693066719962b4da0528dcaed3c717abbbde0 @JPry JPry committed May 12, 2012
Showing with 19 additions and 1 deletion.
  1. +19 −1 init.php
View
20 init.php
@@ -272,7 +272,7 @@ function show() {
echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
break;
case 'checkbox':
- echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', checked( ( $meta == true && $field['std'] ), true, false ), ' />';
+ echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', checked( ( ( $meta == '' && $field['std'] ) || $meta == true ), true, false ), ' />';
echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
break;
case 'multicheck':
@@ -566,4 +566,22 @@ function cmbGetParameterByNameInline(name) {
return $args;
}
+
+add_filter( 'cmb_validate_checkbox', 'cmb_validate_checkbox', 10, 3 );
+/**
+ * Sets the meta value for a stand-alone checkbox to either 1 or 0. Without this, we would have an
+ * empty string in the DB or the word "on". Setting it to either 1 or 0 facilitates checking
+ * whether the box shoudl be chcked, and makes it easier to set a default checked status.
+ *
+ * @param string $new New value to be saved
+ * @param int $post_id ID of the post being saved
+ * @param array $field Array of field values for the checkbox
+ * @return string
+ */
+function cmb_validate_checkbox( $new, $post_id, $field ) {
+ if ( $new === null )
+ return '0';
+ if ( $new == 'on' )
+ return '1';
+}
// End. That's it, folks! //

0 comments on commit e846930

Please sign in to comment.