【发布时间】:2017-02-21 13:09:49
【问题描述】:
我正在使用以下代码来确定复选框是否已被勾选,然后显示一些文本(如果有/没有作为测试)。
选中后,它可以正常工作并显示文本。
当它未选中时,我会在输入框行中收到以下消息,并在下面的代码中添加注释。
非法字符串偏移 'check_checkbox_field_0'
<?php
function webdev_init() {
?>
<h1>Title</h1>
<h2>WedDev Overlay Plugin Options</h2>
<form action='options.php' method='post'>
<h2>Checking</h2>
<?php
settings_fields( 'my_option' );
do_settings_sections( 'checking' );
submit_button();
?>
</form>
<?php
}
function chec_settings_init() {
register_setting( 'my_option', 'chec_settings' );
add_settings_section(
'chec_checking_section',
__( 'Your section description', 'wp' ),
'chec_settings_section_callback',
'checking'
);
add_settings_field(
'chec_checkbox_field_0',
__( 'Settings field description', 'wp' ),
'chec_checkbox_field_0_render',
'checking',
'chec_checking_section'
);
}
function chec_settings_section_callback() {
echo __( 'This section description', 'wp' );
}
function chec_checkbox_field_0_render() {
$options = get_option( 'chec_settings' );
?>
//Error message on line bellow
<input type='checkbox' name='chec_settings[chec_checkbox_field_0]' value='1' <?php if ( 1 == $options['chec_checkbox_field_0'] ) echo 'checked="checked"'; ?> />
<?php
}
$options = get_option( 'chec_settings' );
if ( is_array( $options ) && $options['chec_checkbox_field_0'] == '1' ) {
echo 'Checked';
} else {
echo 'Unchecked';
}
【问题讨论】: