You are saving unchecked checkbox values as empty strings:
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$tp_primo_new_show_below_title = isset($_POST['tp_primo_show_below_title']) ? 'checked' : '';
...
}
So when you fetch the saved unchecked value and then check for if(empty(...))
, it returns true and sets 'checked'
as the default value because empty(...)
:
Returns FALSE if var exists and has a non-empty, non-zero value.
Otherwise returns TRUE.The following things are considered to be empty:
"" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value)
Since get_post_meta
also returns an empty string if no value is found, the only way to distinguish is by saving a separate value for unchecked boxes. So instead of saving 'checked'
and ''
as the meta values, it would be better to use 1
and 0
. This will also take less space in the database.
Edit:
Change the functions as follows:
public function render_metaboxrender_tp_primo_featured_image_options_metabox( $post ) {
// Retrieve an existing value from the database.
...
// Set default values.
// Only non-existent checkbox values will be empty strings, as we are saving '1's and '0's.
// We cannot use the empty function since empty('0') will also return true.
if( $tp_primo_show_below_title == '' ) $tp_primo_show_below_title="1"; //checked by default
if( $tp_primo_page_header_height == '' ) $tp_primo_page_header_height="0"; //unchecked by default
// Form fields.
echo '<p><label><input type="checkbox" name="tp_primo_show_below_title" class="tp_primo_featured_image_locations_field" value="' . $tp_primo_show_below_title . '" ' . checked( $tp_primo_show_below_title, '1', false ) . '> ' . __( 'Show Below Title', 'tp-primo' ) . '</label></p>';
echo '<p><label><input type="checkbox" name="tp_primo_show_above_title" class="tp_primo_featured_image_locations_field" value="' . $tp_primo_show_above_title . '" ' . checked( $tp_primo_show_above_title, '1', false ) . '> ' . __( 'Show Above Title', 'tp-primo' ) . '</label></p>';
echo '<p><label><input type="checkbox" name="tp_primo_set_page_header" class="tp_primo_featured_image_locations_field" value="' . $tp_primo_set_page_header . '" ' . checked( $tp_primo_set_page_header, '1', false ) . '> ' . __( 'Set as Page Header', 'tp-primo' ) . '</label></p>';
echo '<p><label><input type="checkbox" name="tp_primo_page_header_title_overlay" class="tp_primo_featured_image_locations_field" value="' . $tp_primo_page_header_title_overlay . '" ' . checked( $tp_primo_page_header_title_overlay, '1', false ) . '> ' . __( 'Page Header: Title Overlay', 'tp-primo' ) . '</label></p>';
echo '<p><label><input type="checkbox" name="tp_primo_page_header_title_overlay_parallax" class="tp_primo_featured_image_locations_field" value="' . $tp_primo_page_header_title_overlay_parallax . '" ' . checked( $tp_primo_page_header_title_overlay_parallax, '1', false ) . '> ' . __( 'Page Header: Title Overlay/Parrallax', 'tp-primo' ) . '</label></p>';
...
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$tp_primo_new_show_below_title = isset($_POST['tp_primo_show_below_title']) ? '1' : '0';
$tp_primo_new_show_above_title = isset($_POST['tp_primo_show_above_title']) ? '1' : '0';
$tp_primo_new_set_page_header = isset($_POST['tp_primo_set_page_header']) ? '1' : '0';
$tp_primo_new_page_header_title_overlay = isset($_POST['tp_primo_page_header_title_overlay']) ? '1' : '0';
$tp_primo_new_page_header_title_overlay_parallax = isset($_POST[ 'tp_primo_page_header_title_overlay_parallax']) ? '1' : '0';
...
// Update the meta field in the database.
...
}