最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - Cannot save Checkbox meta box value from a Custom Post Type

programmeradmin2浏览0评论

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

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!!

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

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!!

Share Improve this question edited Dec 11, 2019 at 16:08 Subrata Sarkar asked Dec 11, 2019 at 15:38 Subrata SarkarSubrata Sarkar 2395 silver badges16 bronze badges 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 Commented Dec 11, 2019 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? – Subrata Sarkar Commented Dec 11, 2019 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 Commented Dec 11, 2019 at 16:40
Add a comment  | 

1 Answer 1

Reset to default 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 );

    //
    // ...
    //
}
发布评论

评论列表(0)

  1. 暂无评论