在从“常规设置”屏幕打印单选按钮值时,我经常遇到以下错误:
F:\\wamp\\www\\plugin tester\\wp content\\themes\\Twentythine\\functions中的字符串偏移量“service”非法。php在线565
F:\\wamp\\www\\plugin tester\\wp content\\themes\\Twentythine\\functions中的字符串偏移量“service”非法。php在线568
这是我添加到的代码functions.php
文件:
add_filter(\'admin_init\', \'myservice_register_function\');
function myservice_register_function(){
register_setting(\'general\', \'my_service\', \'esc_attr\');
add_settings_field(\'my_service\', \'<label for="service_need">\'.__(\'Do You need My Service\' , \'my_service\' ).\'</label>\' , \'service_function\', \'general\');
}
function service_function(){
$options = get_option( \'my_service\', \'\');
// $options = get_settings( \'my_service\');
if($options[\'service\'] == \'YES\') { //line number 565
echo \'Yes, Service Need\';
}
if($options[\'service\'] == \'NO\') { // line number 568
echo \'No Need Service\';
}
$html = \'<input type="radio" name="my_service[service]" value="YES"/>\';
$html .= \'<label> NEED </label>\';
$html .= \'<input type="radio" name="my_service[service]" value="NO"/>\';
$html .= \'<label > NO NEED </label>\';
echo $html;
}
怎么了?
最合适的回答,由SO网友:brasofilo 整理而成
除了isset
问题,由Rarst指出,在清理功能中有一个错误。esc_attr
似乎无法使用单选按钮,使用esc_sql
完成工作
您还缺少checked
按钮状态:
add_filter( \'admin_init\', \'myservice_register_function\' );
function myservice_register_function()
{
register_setting( \'general\', \'my_service\', \'esc_sql\' );
add_settings_field(
\'my_service\',
\'<label for="service_need">\'.__(\'Do You need My Service\' , \'my_service\' ).\'</label>\' ,
\'service_function\',
\'general\'
);
}
function service_function()
{
$options = get_option( \'my_service\', \'\');
$yes = $no = \'\';
if( isset( $options[\'service\'] ) )
{
if( $options[\'service\'] == \'YES\')
{
echo \'Yes, Service Need<br />\';
$yes = \' checked="checked"\';
}
if( $options[\'service\'] == \'NO\')
{
echo \'No Need Service<br />\';
$no = \' checked="checked"\';
}
}
?>
<input type="radio" name="my_service[service]" value="YES"<?php echo $yes; ?> />
<label> NEED </label>
<input type="radio" name="my_service[service]" value="NO"<?php echo $no; ?> />
<label> NO NEED </label>
<?php
}