【发布时间】:2016-04-20 18:04:50
【问题描述】:
我正在为 Wordpress 制作主题选项,并且正在尝试在一个设置字段中创建多个复选框选项。然而,只有一个复选框值被保存。经过一番搜索,我意识到多个复选框需要以不同的方式保存,而不是单个复选框。但我似乎无法找到关于如何做到这一点的直接答案。
这是我的标记。
<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>
这就是我为单个复选框所做的。所以我的问题是如何保存所有提交的复选框并在它们上回显选中的值?根据我的研究,我需要将值保存在数组中并检查它是否在数组中,但我对 php 没有经验,所以不完全理解我在做什么。任何帮助将不胜感激。
编辑:
我已尝试应用可能重复帖子中的答案。但这些答案似乎根本不起作用。应用这些答案后,复选框均未保存。也许我以错误的方式应用它?
<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;
}
}
?>
就像我说的我对 php 没有经验。
编辑:
据我了解,我需要将输入名称设置为
<input name="The section on which this option will be displayed[ID used to identify the field]" />
所以我的完整代码是
//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
像这样设置输入字段
<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"'; } ?> />
或类似的东西
<input type='checkbox' name='myCB[]' value='cheese sauce'>
什么也不做
<?php
if(!empty($_POST['mbpanel_display_section_options'])) {
foreach($_POST['mbpanel_display_section_options'] as $check) {
echo $check;
}
}
?>
我什至不明白它的意思。抱歉,如果我在这里很痛苦,但我花了一整天的时间试图让它发挥作用,而我尝试过的一切都没有奏效,我似乎无法将类似的实例应用于这个特定案例。同样,当我阅读诸如“将其设为数组”之类的内容时,我不知道那是什么意思。
【问题讨论】:
-
将输入名称更改为 />
标签: php wordpress checkbox http-post