1

在我尝试验证设置之前,插件管理面板工作正常;

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 行中删除 // array($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 更改为 &$this 才能正常工作。我试过了,什么也没发生,同样的错误。

有任何想法吗?

另外:是否有必要为我要添加的每个附加字段使用 add_settings_field ?好像我会添加大量的这些。

var_dump 向我展示了这个:

string(31) "{$this->options[jo_col_1_name]}"
4

1 回答 1

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(){
}
?>
于 2013-09-07T13:18:24.567 回答