I'm using the following code to determine if a checkbox has been ticked and then display some text if it has/hasn't as a test.
When its checked, it works fine and the text displays.
When its unchecked I get the message below on the two lines I have commented in my code below.
Illegal string offset 'chec_checkbox_field_0'
<?php
function webdev_init() {
?>
<h1>Title</h1>
<h2>WedDev Overlay Plugin Options</h2>
<form action='options.php' method='post'>
<h2>Checking</h2>
<?php
settings_fields( 'my_option' );
do_settings_sections( 'checking' );
submit_button();
?>
</form>
<?php
}
function chec_settings_init() {
register_setting( 'my_option', 'chec_settings' );
add_settings_section(
'chec_checking_section',
__( 'Your section description', 'wp' ),
'chec_settings_section_callback',
'checking'
);
add_settings_field(
'chec_checkbox_field_0',
__( 'Settings field description', 'wp' ),
'chec_checkbox_field_0_render',
'checking',
'chec_checking_section'
);
}
function chec_settings_section_callback() {
echo __( 'This section description', 'wp' );
}
function chec_checkbox_field_0_render() {
$options = get_option( 'chec_settings' );
?>
//Error message on line bellow
<input type='checkbox' name='chec_settings[chec_checkbox_field_0]' value='1' <?php if ( 1 == $options['chec_checkbox_field_0'] ) echo 'checked="checked"'; ?> />
<?php
}
$options = get_option( 'chec_settings' );
//Error message on line bellow
if ( $options['chec_checkbox_field_0'] == '1' ) {
echo 'Checked';
} else {
echo 'Unchecked';
}