Plugin Options with Just Checkboxes Fails Validation

I am developing an ‘private’ plugin that will have just checkboxes on the plugin settings screen. The relevant code below is used (some code not there, but all parts of the plugin code are in place). Assume that other parts of the plugin are properly coded…the option page is rendered, the checkbox options are rendered, the proper add_actions are in place.

<?php

add_action( ‘admin_menu’, ‘the_add_admin_menu’ );
add_action( ‘admin_init’, ‘the_settings_init’ );

function the_add_admin_menu( ) {
// code that adds the options page (assume OK; code not relevant)
}

function the_settings_init( ) {
register_setting( ‘pluginPage’, ‘the_settings’,’the_settings_section_callback’ );

add_settings_section(
‘the_pluginPage_section’,
__( ‘Some text’, ‘CSPF_namespace’ ),
‘the_settings_section_callback’,
‘pluginPage’
);

// first checkbox
add_settings_field(
‘the_checkbox_1’,
__( ‘1’, ‘the_namespace’ ),
‘the_checkbox_1_render’,
‘pluginPage’,
‘the_pluginPage_section’
);

// first checkbox
add_settings_field(
‘the_checkbox_2’,
__( ‘2’, ‘the_namespace’ ),
‘the_checkbox_2_render’,
‘pluginPage’,
‘the_pluginPage_section’
);
}

// render checkbox 1
function the_checkbox_1_render( ) {
$options = get_option( ‘Plugin_Checkboxes’ );
echo ‘<p><input type=”checkbox” id=”the_checkbox_1″ name=”the_checkbox_1″ value=”1″‘ . checked( 1, $options[‘the_checkbox_1’], false ) . ‘/></p>’;
}
// render checkbox 2
function the_checkbox_2_render( ) {
$options = get_option( ‘Plugin_Checkboxes’ );
echo ‘<p><input type=”checkbox” id=”the_checkbox_2″ name=”the_checkbox_2″ value=”1″‘ . checked( 1, $options[‘the_checkbox_2’], false ) . ‘/></p>’;
}

// validate all input; $new_input will be stored in the options table
function the_settings_section_callback($input)
{
$new_input = array(); // to store the validated input
// validate checkbox 1
$new_input[‘the_checkbox_1’] = (isset($input[‘the_checkbox_1’]) && !empty($input[‘the_checkbox_1’])) ? 1 : 0;
// validate checkbox 2
$new_input[‘the_checkbox_2’] = (isset($input[‘the_checkbox_2’]) && !empty($input[‘the_checkbox_2’])) ? 1 : 0;
return $new_input;
}

The problem: if none of the checkboxes are checked, then the option_value field in the wp_options table is empty, so the validation check (the_settings_section_callback function) fails with a message “Warning: Illegal string offset ‘the_checkbox_1’….” message, which is displayed just above the rendering.

And, the checkbox settings are not saved in the option_value field; the option_name row is there.

How do I ensure that in an options page that has only checkboxes, when no checkboxes are selected, the option_value field will still contain an array so the validation will work? How do I ensure that the option_value contains an empty array if all checkboxes are not selected? (I have spent hours wandering though here and the googles…)

Read more here:: Plugin Options with Just Checkboxes Fails Validation

Leave a Reply

Your email address will not be published. Required fields are marked *