I am getting some errors when trying to create a checkbox within a simple plugin I am creating. I have a simple settings page at the moment. The errors are:

Illegal string offset 'single_images'


Undefined index: single_images_zoom

Here is the checkbox code. Any help would be appreciated.

<input id="iwmp_settings['single_images']" name="iwmp_settings['single_images']" type="checkbox" value="1" <?php checked(1, $iwmp_options['single_images'], false); ?> />
<label class="description" for="iwmp_settings['single_images']"><?php _e('Use with Single Images', 'iwmp_domain'); ?></label>

<input id="iwmp_settings['single_images_zoom']" name="iwmp_settings['single_images_zoom']" type="checkbox" value="1" <?php checked(1, $iwmp_options['single_images_zoom'], false); ?> />
                    <label class="description" for="iwmp_settings['single_images_zoom']"><?php _e('Use Zoom effect with Single Images', 'iwmp_domain'); ?></label>

The Illegal string offset issue seems to be caused from this snippet:

<?php checked(1, $iwmp_options['single_images'], false); ?>


1 Answer 1


You shouldn't put quotes around the index for HTML names - otherwise you end up with 'single_images' (including single quotes) as the PHP name (so you'd have to address it $iwmp_options["'single_images'"], which some might consider confusing) ... eg:

add_action( 'admin_init', function () {
    register_setting( 'iwmp_settings', 'iwmp_settings' );

    add_settings_section( 'single_images', __( 'Single Images', 'iwmp_domain' ) , function () {
        $iwmp_options = get_option( 'iwmp_settings' );
        // Note quotes around the PHP index, no quotes around the HTML index.
        // (Also id's should basically be alphanumeric with underscores/hyphens (for pre-HTML5 compatibility anyway).)
        <input id="iwmp_settings_single_images_" name="iwmp_settings[single_images]" type="checkbox" <?php checked( isset( $iwmp_options['single_images'] ) ); ?> />
        <label class="description" for="iwmp_settings_single_images_"><?php _e('Use with Single Images', 'iwmp_domain'); ?></label>
        <br />
        <input id="iwmp_settings_single_images_zoom_" name="iwmp_settings[single_images_zoom]" type="checkbox" <?php checked( isset( $iwmp_options['single_images_zoom'] ) ); ?> />
        <label class="description" for="iwmp_settings_single_images_zoom_"><?php _e('Use Zoom effect with Single Images', 'iwmp_domain'); ?></label>
    }, 'iwmp_settings' );
} );

add_action( 'admin_menu', function () {
    add_options_page( __( 'IWMP Settings', 'iwmp_domain' ), __( 'IWMP', 'iwmp_domain' ), 'manage_options', 'iwmp_settings', function() {
        <div class="wrap">
            <h2><?php _e( 'IWMP Settings', 'iwmp_domain' ); ?></h2>
            <form method="post" action="options.php">
                <?php settings_fields( 'iwmp_settings' ); ?>
                <?php do_settings_sections( 'iwmp_settings' ); ?>
                <p class="submit">
                    <input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'iwmp_domain' ) ?>" />
    } );
} );

Also, just checking for isset() here on checkbox values makes things simple. And if you want to manipulate them then set a variable eg $single_images = isset( $iwmp_options['single_images'] ); etc. or use a wrapper function. (And as a side note for shortcode compatibility I find 1/0 handier then true/false.)

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.