Checkbox fields in WordPress Metaboxes a solution for NULL values

Talking about forms, one of the most common issues is that checkbox fields don’t return any value when they are not checked. We just receive their values -usually TRUE- when they are checked but if they are unchecked they ‘say’ nothing. What can we do?

Checkbox fields in WordPress Metaboxes a solution for NULL values

This behavior of checkboxes fields is not an error, long time ago they was designed in that way (they are silent) however, in most cases,  this behaviour is a problem because the lacking of value doesn’t allow developers to know whether there is not actually a value, or if the value is actually FALSE. There is not difference between NOTHING and FALSE so for example, examining the meta fields of a post, the programmer can’t distinguish whether this post has not been updated yet, or if the post has been updated but the value of its meta field is equal to FALSE.

Despite of this default lacking of unchecked value, there are several solutions that we can apply. Today, we’re going to propose one of them and, specificially, one of them adapted for using inside WordPress Metaboxes.

Continuing with the example of yesterday (How to add a Metabox. A step by step approach.), now we’re going to add a checkbox field for controlling whether this person lives in CA or not, so we add a simple checkbox field to our function render_metabox_callback_function. Something like that,

<?php
function render_metabox_callback_function ( $post, $data ) {

    //... yesterday's code

    echo '<p><label for="isCAClient">Do you live in CA?&nbsp;';
    echo '<input value="1" name="isCAClient" id="isCAClient" ' . checked ( TRUE, $iscaclient, false ) . ' type="checkbox">';
    echo '</label><p>';

    //... yesterday's code

}
?>
A checkbox Field in a WordPress Metabox, an example

The problem with the unckeched checkbox fields

Ok, the field is already operative but we have got a problem. See in detail the next process.

  1. while the field remains unchecked, the value would be always correct, ie, the lacking of value could be interpreted as FALSE –perfect!–.
  2. if one day we mark this field as checked (the person begins to live in CA ) the saving function saving_the_metabox_data will receive a TRUE value and will save the new value –perfect!–.
  3. but, if another day in future, we need change this field to the FALSE value again, it will be impossible to change it. It always remains as TRUE… –error– .

What is happening is that an unchecked checkbox field never sends a value for the FALSE state so, the function that save the meta values saving_the_metabox_data never receives this change… Remember the code for saving data

    // YOU NEVER PASS THIS CHECK POINT BECAUSE THE FUNCTION DOESN'T RECEIVES ANY VALUE 
    // IN CASE OF 'FALSE' STATE FOR CHECKBOX FIELDS SO, THE FUNCTION WILL NOT BE EVER ABLE 
    // TO UPDATE THE VALUE IN DATABASE. THE VALUE iscaclient ALWAYS REMAINS TRUE...

    if ( isset( $_REQUEST['isCAClient'] ) )    // never set
        update_post_meta( $post_id, 'iscaclient',  $_REQUEST['isCAClient'] );

So, next time that you reload the posts edit screen after pressing the update button, you’ll notice that the field remains checked, so unchanged.

The solution for the unchecked checkbox fields

The simplest solution to force always a real value (TRUE or FALSE) is sent, instead of (TRUE or nothing), is just to add a new hidden field before our former checkbox field. This new hidden field has to have the same attribute name and its attribute value has to be always FALSE. Something like that.

<?php
function render_metabox_callback_function ( $post, $data ) {

    //... yesterday's code

    // we add a new hidden field before the real checkbox field
    echo '<input value="0" name="isCAClient" type="hidden">';

    echo '<p><label for="isCAClient">Do you live in CA?&nbsp;';
    echo '<input value="1" name="isCAClient" id="isCAClient" ' . checked ( TRUE, $iscaclient, false ) . ' type="checkbox">';
    echo '</label><p>';

    //... yesterday's code

}
?>

Doing this we get that the saving function always receives a value because, when the checkbox field is unchecked (nothing) then the hidden field will be sent by the browser and, when the checkbox field is checked, due to is inserted after the hidden field, it overwrites the FALSE value of hidden field for its own value TRUE and the browser will sent TRUE. In any case the saving function will receive a VALUE so, we’ll pass the isset() check point and in consequence, the function saving_the_metabox_data will update the meta value in the database with a FALSE value.

Indeed, there are other solutions for the lack of value of checkbox fields, however this solution is very simple and requires very little additional code and very little subsequent control. I hope it helps you.

Have a nice WordPressing!

1 Comment on Checkbox fields in WordPress Metaboxes a solution for NULL values

Leave a Reply to Examples of adding fields to WordPress Metabox — What about WordPress Cancel Replay

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>