add_settings_field not functioning with validation – returns string

The plugin admin panel works fine until I attempt to validate the settings;

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');

So if I remove the // array($this, ‘jo_validate_settings’) // from the register_setting line my text boxes function fine. But when I add that validation code, it breaks them and returns this error;

Read More

*Warning: Illegal string offset ‘jo_col_1_name’ in /Applications/MAMP/htdocs/wp-content/plugins/plugin-rough.php on line 82*

The boxes are here:

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']}' />"; }

I did some reading online and found something semi relevant, someone saying that the keys were the same and I needed to change $this to &$this for it to function. I tried it and nothing happened, same errors.

Any ideas?

Additionally: Is it necessary to use add_settings_field for each additional field I want to add? Seems like I will be adding a very large amount of these.

var_dump shows me this:

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

Related posts

Leave a Reply

1 comment

  1. Try something like this:

    if (!isset($youroptions['somename'])){
    $youroptions['somename'] = '';
    }  
    //or like this
    if (!isset($youroptions['somename'])){
     $youroptions['somename']= 'blank';//You can try to use 
    }                                  //$youroptions['somename']= 'undefined'; 
    

    Full working example (added inside “Settings” backend) :

    <?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(){
    }
    ?>