1

I have created a simple Custom Post Type which has a few textboxes and one checkbox. Textbox values are getting saved and updated perfectly in wp_postmeta table but I am not being able to save the Checkbox value.

Below is my code:

function clients_custom_metaboxes() {
    // StreetAddress
    add_meta_box(
        'clients_street',
        __( 'Street Address' ),
        'infotravel_clients_street_metabox',
        'clients'
    );

    ...

    //Visibility
    add_meta_box(
        'clients_visible',
        __( 'Show on clients page' ),
        'infotravel_clients_visible_metabox',
        'clients'
    );
}

add_action( 'add_meta_boxes', 'clients_custom_metaboxes' );

function infotravel_clients_street_metabox( $post ) {
    wp_nonce_field( 'clients_street', 'clients_street_nonce' );
    $value = get_post_meta( $post->ID, '_clients_street', true );
    ?>
    <input class="trek_inputs required" value="<?php echo $value ?>" type="text" id="txtClientsStreet" name="txtClientsStreet" maxlength="200" required placeholder="Street address" />
    <?php
}

function infotravel_clients_visible_metabox( $post ) {
    wp_nonce_field( 'clients_show', 'clients_show_nonce' );
    $value = get_post_meta( $post->ID, '_clients_show', true );
    ?>
    <input type="checkbox" id="chkClientShow" name="chkClientShow" value="<?php echo $value ?>" />
    <label for="chkClientShow">Show this client on Clients page</label>
    <?php
}


function infotravel_save_clients_meta( $post_id ) {
    if(
        ! isset( $_POST['clients_street_nonce'] ) ||
        ! isset( $_POST['clients_show_nonce'] ) ) {
        return;
    }

    if( ! current_user_can( 'edit_post', $post_id) ) {
        return;
    }

    // mandatory fields
    $street_address = sanitize_text_field( $_POST['txtClientsStreet']  );
    $visible        = sanitize_text_field( $_POST['chkClientShow'] );

    $short_address  = $street_address . ', ' . $city . ', ' . $state;
    $full_address   = $short_address . ', ' . $zip . ', ' . $country;

    /**
     * Update meta information
     */

    update_post_meta( $post_id,  '_clients_street', $street_address );
    update_post_meta( $post_id,  '_clients_show', $visible );
    update_post_meta( $post_id,  '_clients_short_address', $short_address );
    update_post_meta( $post_id,  '_clients_full_address', $full_address );
}

add_action( 'save_post',  'infotravel_save_clients_meta' );

What I am missing?

DATABASE SCREENSHOT

enter image description here

Further Testing

I added a value (xxxxxxx) in _clients_show field and checked what I am getting when metabox is created:

function infotravel_clients_visible_metabox( $post ) {
    wp_nonce_field( 'clients_show', 'clients_show_nonce' );
    $value = get_post_meta( $post->ID, '_clients_show', true );
    print_r($value);
    ?>
    <input type="checkbox" id="chkClientShow" name="chkClientShow" checked="checked" />
    <label for="chkClientShow">Show this client on Clients page</label>
    <?php
}

Surprisngly, the value is not displaying!!

3
  • Have you tried logging the $_POST output? You are trying to run sanitize_text_field() on the checkbox value, which may be causing the problem. Try just outputting its value first. A checkbox is a boolean, so you shouldn't need to sanitize it at all.
    – WebElaine
    Dec 11 '19 at 15:50
  • Actually I tried without sanitization and it didn't work. I also tried to print the value by echo-ing $_POST['chkClientShow'] inside the function hooked to save_post action but nothing got displayed. Maybe I am trying to print it at a wrong place? Dec 11 '19 at 15:55
  • Right, you can't echo anything in that hook because it's never actually displaying on the screen. Instead you could fopen() and fwrite() the variable to a temporary text log to see what prints out.
    – WebElaine
    Dec 11 '19 at 16:40
1

In my opinion the problem is the way you operate the checkbox. As the checkbox value, you set the value read from the database, which is sometimes an empty string.

When you send an unchecked checkbox, there is no $_POST['chkClientShow'] key and an empty string is saved to the database. Then the empty string is set as the checkbox value. From now, the custom field _clients_show always will be an empty string.

The following code should work

function infotravel_clients_visible_metabox( $post ) {
    wp_nonce_field( 'clients_show', 'clients_show_nonce' );
    $value = get_post_meta( $post->ID, '_clients_show', true );
    $is_checked = ($value == 1) ? 'checked' : '';
    ?>
    <input type="checkbox" id="chkClientShow" name="chkClientShow" value="1" <?php echo $is_checked; ?>/>
    <label for="chkClientShow">Show this client on Clients page</label>
    <?php
}
function infotravel_save_clients_meta( $post_id ) 
{
    //
    // ...
    //

    $visible = isset( $_POST['chkClientShow'] ) && $_POST['chkClientShow'] == 1;
    $visible = (int)$visible;
    update_post_meta( $post_id,  '_clients_show', $visible );

    //
    // ...
    //
}
1
  • Thanks! I however, had to change $is_checked = ($value == 1) ? 'checked' : ''; to $is_checked = ((int)$value == 1) ? 'checked' : ''; Dec 12 '19 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.