I’m driving myself crazy here trying to get a checkbox to save on a plugin settings page.
Right now, as a template, I’m working through the excellent example given here: Settings API with arrays example
I’m also referring to a very useful tutorial at https://www.smashingmagazine.com/2016/04/three-approaches-to-adding-configurable-fields-to-your-plugin/ with a detailed git at https://github.com/rayman813/smashing-custom-fields/blob/master/smashing-fields-approach-1/smashing-fields.php
I’ve also read about a half dozen posts with similar questions but none have my answer, at least that I can find and understand.
I think I’m stumbling on the syntax of a checked() function.
— Updated code —
Here are the key pieces of code:
The overall data in the function that calls register_setting(), add_settings_section() and add_settings_fields
$option_name = 'rpq_plugin_option_name'; // free to choose this name
// Fetch existing options.
$option_values = get_option( $option_name );
$default_values = array (
'number' => 500,
'color' => 'blue',
'long' => '',
'activity' => array(
'baseball' => '',
'golf' => '',
'hockey' => '')
);
$data = shortcode_atts( $default_values, $option_values );
The add_settings_field() for the checkboxes:
add_settings_field(
'section_2_field_2',
'Checkboxes',
'rpq_plugin_render_section_2_field_2', // callback to render html
'rpq-default-settings', // menu slug
'section_2',
array (
'label_for' => 'label4', // makes the field name clickable,
'name' => 'activity', // value for 'name' attribute
'value' => array_map( 'esc_attr', $data['activity'] ) ,
'options' => array (
'baseball' => 'Baseball',
'golf' => 'Golf',
'hockey' => 'Hockey'
),
'option_name' => $option_name
)
);
The callback to render the html:
function rpq_plugin_render_section_2_field_2( $args )
{
$options_markup = '';
$iterator = 0;
$values = $args['value'];
$options = $args['options'];
rpq_plugin_debug_var( $options, 'Options: ' );
rpq_plugin_debug_var( $values, 'Values: ' );
foreach ( $args['options'] as $key => $title ) {
rpq_plugin_debug_var( $key, 'Key: ' );
rpq_plugin_debug_var( $title, 'Title: ' );
$checked = checked((in_array($title, $values)), true, false);
rpq_plugin_debug_var($checked, 'Checked: ');
$iterator ++;
$options_markup .= sprintf(
'<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[%2$s]" type="checkbox" value="%3$s" %4$s /> %5$s</label><br/>',
$args['option_name'],
$args['name'],
$key,
$checked,
$title,
$iterator
);
}
printf( '<fieldset>%s</fieldset>', $options_markup );
//print '</select>';
rpq_plugin_debug_var( $values, 'Values: ');
}
The output from the debug dump of the initial variables is as I’d expect:
Options: = array (
'baseball' => 'Baseball',
'golf' => 'Golf',
'hockey' => 'Hockey',
)
Values: = array (
'baseball' => '',
'golf' => '',
'hockey' => '',
)
Key: = 'baseball'
Title: = 'Baseball'
Checked: = ''
Key: = 'golf'
Title: = 'Golf'
Checked: = ''
Key: = 'hockey'
Title: = 'Hockey'
Checked: = ''
After trying to save, i get:
Values: = NULL
I’ve tried a host of variations on ways to get cjhecked set but I can’t get the checkboxes to save.