Option-1: Change the option from show
to hide
:
The easiest way would be to change the naming and behaviour of the option
. So instead of $options['classy_show_resume']
, you may implement it as $options['classy_hide_resume']
.
That way, by default hide option
is not checked, that means you show it by default. Then hide it when this hide option
is checked.
So simply by changing the name of the option, and then implementing hide option
(instead of the show option
) in your theme, you can fix it easily.
So your CODE will be like:
function classy_hide_resume_section_callback () {
$options = get_option( 'classy_general_settings' );
$html = "<label class="switch">";
if ( empty( $options['classy_hide_resume'] ) ) $options['classy_hide_resume'] = 0;
$html .= "<input type="checkbox" name="classy_general_settings[classy_hide_resume]" value="1" " . checked($options['classy_hide_resume'], 1, false) . " >";
$html .= "<div class="slider round"></div>";
$html .= "</label>";
echo $html;
}
Option-2: Make classy_show_resume
checked by default:
If you must use classy_show_resume
with default to checked, then you may use the following logic:
By default get_option( 'classy_general_settings' )
should return false
and when it’s saved as unchecked
, it’ll either be an empty string or an array with $options['classy_show_resume']
not set. So we’ll use this to determine the default value vs. saved checked
or unchecked
value.
According to this, your new CODE will be like:
function classy_hide_resume_section_callback () {
$options = get_option( 'classy_general_settings' );
$classy_show_resume = 0;
if ( $options === false ) {
// nothing is set, so apply the default here
$classy_show_resume = 1;
}
else if( is_array( $options ) && isset( $options['classy_show_resume'] ) ) {
// classy_show_resume is checked
$classy_show_resume = $options['classy_show_resume'];
}
$html = "<label class="switch">";
$html .= "<input type="checkbox" name="classy_general_settings[classy_show_resume]" value="1" " . checked( $classy_show_resume, 1, false ) . " >";
$html .= "<div class="slider round"></div>";
$html .= "</label>";
echo $html;
}
Now it should be checked by default and show the correct checked
| unchecked
status after saving.