插件管理面板正常工作,直到我尝试验证设置;
register_setting('jo_plugin_options', 'jo_plugin_options', array($this, 'jo_validate_settings'));
add_settings_section('jo_main_section', 'Data Table Settings', array($this, 'jo_main_section_cb'), __FILE__); // id, title, section, callback, page
add_settings_field('jo_col_1_name', 'Column 1 Name: ', array($this, 'jo_col_1_name_setting'), __FILE__, 'jo_main_section');
add_settings_field('jo_col_2_name', 'Column 2 Name: ', array($this, 'jo_col_2_name_setting'), __FILE__, 'jo_main_section');
add_settings_field('jo_col_3_name', 'Column 3 Name: ', array($this, 'jo_col_3_name_setting'), __FILE__, 'jo_main_section');
add_settings_field('jo_file_upload', 'Upload File: ', array($this, 'jo_file_upload_setting'), __FILE__, 'jo_main_section');
因此,如果我从register_setting行中删除// 数组($ this,'jo_validate_settings') //,我的文本框功能正常。但是当我添加验证代码时,它会破坏它们并返回此错误;
* 警告:第82行/Applications/MAMP/htdocs/wp-content/plugins/plugin-rough.php中的非法字符串偏移'jo_col_1_name'
方框在这里:
public function jo_col_1_name_setting() {
echo "<input id='jo_col_1_name' name='jo_plugin_options[jo_col_1_name]' type='text' value='{$this->options['jo_col_1_name']}' />"; }
public function jo_col_2_name_setting() {
echo "<input name='jo_plugin_options[jo_col_2_name]' type='text' value='{$this->options['jo_col_2_name']}' />"; }
public function jo_col_3_name_setting() {
echo "<input name='jo_plugin_options[jo_col_3_name]' type='text' value='{$this->options['jo_col_3_name']}' />"; }
我在网上做了一些阅读,发现了一些半相关的东西,有人说密钥是相同的,我需要将$ this更改为&amp; $ this才能运行。我试过了,什么都没发生,同样的错误。
有什么想法吗?
此外:是否有必要为我想添加的每个附加字段使用add_settings_field?好像我将添加大量的这些。
var_dump告诉我这个:
string(31) "{$this->options[jo_col_1_name]}"
答案 0 :(得分:1)
尝试这样的事情:
if (!isset($youroptions['somename'])){
$youroptions['somename'] = '';
}
//or like this
if (!isset($youroptions['somename'])){
$youroptions['somename']= 'blank';//You can try to use
} //$youroptions['somename']= 'undefined';
完整的工作示例(在“设置”后端添加):
<?php
add_action('admin_menu','create_theme_options_page');
function create_theme_options_page(){
add_options_page('Theme Options','Theme Options','administrator',__FILE__,'build_options_page');
}
function build_options_page(){
?>
<div id="theme-options-wrap">
<div style="float:left;margin-top:19px;position:absolute;" class="icon32" id="icon-tools"><br/></div>
<h2>My options page</h2>
<form method="post" action="options.php">
<?php settings_fields('plugin_options');?>
<?php do_settings_sections(__FILE__); ?>
<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save changes');?>"/>
</p>
</form>
</div>
<?php
}
add_action('admin_init','register_and_build_fields');
function register_and_build_fields(){
register_setting('plugin_options','plugin_options','validate_setting');
add_settings_section('main_section','<br/><br/>Main Settings','section_cb',__FILE__);
add_settings_field('banner_headings','Banner Heading:','banner_heading_settings',__FILE__,'main_section');
}
//Banner Heading
function banner_heading_settings(){
$options = get_option('plugin_options');
if(isset( $options['banner_headings'])) {
$options = $options['banner_headings'];
} else {
$options='';
}
echo '<input type="text" name="plugin_options[banner_headings]" value="'.$options.'" />';
}
//Return our options back
function validate_setting($plugin_options){
return $plugin_options;
}
function section_cb(){
}
?>