I’m making a theme options for WordPress and I’m trying to create a multiple checkbox option in one settings field. However only one checkbox value is being saved. After some searching around I’ve realized that multiple check-boxes need to be saved differently then single check-boxes. But i can’t seem to find a straight answer on how to do this.
Here’s my markup.
<div class="checkbox-toggle">
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_date">
<div>
<span>Off</span>
<span>On</span>
</div>
</label>
<p class="input-description">Date</p>
</div>
<div class="checkbox-toggle">
<input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_author">
<div>
<span>Off</span>
<span>On</span>
</div>
</label>
<p class="input-description">Author</p>
</div>
That’s how I’ve been doing it for single checkboxes. So my question is how can i save all of the check-boxes that get submitted and echo the checked value on them ? From what I’ve researched i need to save the values in an array and check if it’s in the array but I’m not experienced with php so don’t fully understand what I’m doing. Any help would be greatly appreciated.
EDIT:
I’ve tried applying the answers from the possible duplicate post. But those answers don’t seem to work at all. After applying those answers neither checkboxes are saved. Maybe I’m applying it in the wrong way ?
<div class="checkbox-toggle">
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> />
<label for="show_date"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Date</p>
</div>
<div class="checkbox-toggle">
<input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> />
<label for="show_author"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Author</p>
</div>
<?php
if(!empty($_POST['mbpanel_display_section_options'])) {
foreach($_POST['mbpanel_display_section_options'] as $check) {
echo $check;
}
}
?>
Like i said i’m not experienced with php.
EDIT:
From what i understand i need to set the input name as
<input name="The section on which this option will be displayed[ID used to identify the field]" />
So my full code is
//Initialize the Display Options page by registering the Sections, Fields, and Settings
function mbpanel_initialize_display_options() {
// If the display options don't exist, create them
if( false == get_option( 'mbpanel_display_section_options' ) ) {
add_option( 'mbpanel_display_section_options' );
} // end if
// Register Display Options section
add_settings_section(
'display_settings_section_id', // ID used to identify this section and with which to register options
'Display Options', // Title to be displayed on the administration page
'mbpanel_display_options_callback', // Callback used to render the description of the section
'mbpanel_display_section_options' // Section on which to add this section of options
);
// Add Display Options section fields
add_settings_field(
'show_meta', // ID used to identify the field throughout the theme
'Meta', // The label to the left of the option interface element
'mbpanel_toggle_meta_callback', // The name of the function responsible for rendering the option interface
'mbpanel_display_section_options', // The section on which this option will be displayed
'display_settings_section_id' // The name of the section to which this field belongs
);
// Register Display Options fields
register_setting(
'mbpanel_display_section_options', // Group/Page this setting belongs to
'mbpanel_display_section_options' // ID of the field being registered
);
} // end mbpanel_initialize_display_options
add_action('admin_init', 'mbpanel_initialize_display_options');
/* ------------------------------------------------------------------------ *
* Display Options Section Callbacks
* ------------------------------------------------------------------------ */
// Display Options section description
function mbpanel_display_options_callback() {
echo '<p class="page-description">Select which areas of content you wish to display.</p>';
} // end mbpanel_display_options_callback
/* ------------------------------------------------------------------------ *
* Display Options Field Callbacks
* ------------------------------------------------------------------------ */
// Render the Display Options interface elements for toggling the visibility of the header element
function mbpanel_toggle_meta_callback() {
// First, we read the options collection
$options = get_option('mbpanel_display_section_options');
// Next, we update the name attribute to access this element's ID in the context of the display options array
// We also access the show_header element of the options collection in the call to the checked() helper function
?>
<div class="option-wrap">
<div class="checkbox-toggle">
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta]" <?php if(1 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_date"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Date</p>
</div>
<div class="checkbox-toggle">
<input id="show_author" type="checkbox" value="2" name="mbpanel_display_section_options[show_meta]" <?php if(2 == $options['show_meta']) echo 'checked="checked"'; ?> />
<label for="show_author"><div><span>Off</span><span>On</span></div></label>
<p class="input-description">Author</p>
</div>
</div>
<p class="option-description">Choose which post meta info you wish to display.</p>
<?php
} // end mbpanel_toggle_meta_callback
Setting the input field like
<input id="show_date" type="checkbox" value="1" name="mbpanel_display_section_options[show_meta][]" <?php if(isset($_POST['mbpanel_display_section_options'])){ echo 'checked="checked"'; } ?> />
or something like
<input type='checkbox' name='myCB[]' value='cheese sauce'>
does nothing nor does
<?php
if(!empty($_POST['mbpanel_display_section_options'])) {
foreach($_POST['mbpanel_display_section_options'] as $check) {
echo $check;
}
}
?>
nor do i even understand what it’s meant to do. Sorry if i’m being a pain here but i’ve spent all day trying to get this to work and everything i’ve tried hasn’t worked, i can’t seem to apply similar instances to this specific case. also when i read things like “make it an array” i have no idea what that means.
You need ot make your checkbox name an array
<input type='checkbox' name='myCB[]' value='cheese sauce'>
Then you can get them on the other side from the
$_POST['myCB']
array (it’s an array).