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 |1 Answer
Reset to default 1In 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 );
//
// ...
//
}
$_POST
output? You are trying to runsanitize_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:50echo
-ing$_POST['chkClientShow']
inside the function hooked tosave_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:55fopen()
andfwrite()
the variable to a temporary text log to see what prints out. – WebElaine Commented Dec 11, 2019 at 16:40