0

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';
}

2 Answers 2

1
$options = get_option( 'chec_settings' );
//Error message on line bellow
if ( is_array( $options ) && $options['chec_checkbox_field_0'] == '1' ) {
    echo 'Checked';
} else {
    echo 'Unchecked';
}

if the value is not checked it will not save in db. So check if the $options is array.

4
  • array_key_exists() expects parameter 2 to be array, string given in /home/admarkco/public_html/admarkdigital.co.uk/subdomains/hglass/wp-content/plugins/Wordpress-Plugin/webdev.php on line 94 . I have to go to the settings page again and recheck the box for the error to go away. And it comes up every time the box is checked for the first time.
    – Jason
    Commented Feb 21, 2017 at 12:02
  • also still get first error message when unchecked
    – Jason
    Commented Feb 21, 2017 at 12:03
  • sorry i am fail. see updated code. check if options is array rather then assuming its already an array.
    – Anwer AR
    Commented Feb 21, 2017 at 12:15
  • I still get Warning: Illegal string offset 'chec_checkbox_field_0' in /home/admarkco/public_html/admarkdigital.co.uk/subdomains/hglass/wp-content/plugins/Wordpress-Plugin/webdev.php on line 87 /> when unchecked . The error refers to the input line
    – Jason
    Commented Feb 21, 2017 at 12:42
1

You need to check for the existance of the key, before using it in a condition.

Change:

<?php if ( 1 == $options['chec_checkbox_field_0'] ) echo 'checked="checked"'; ?>

...to:

<?php if (isset($options['chec_checkbox_field_0']) && (1 == $options['chec_checkbox_field_0'])) echo 'checked="checked"'; ?>

This way, if the checkbox is unchecked and / or the option isn't actually set, the first condition will fail, and PHP will immediately disregard the rest of the statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.