wordpress settings API and option array structure

how do you get the settings API to save options that are in an array structure like this:

$array = array (
                array(
             'id'=> '1',
             'name'=> 'tom',
             'pageurl'=> 'someurl',
             'notes'=> 'someNotes'
              )
                array(
                 'id'=>'2',
                 'name'=>'harry',
                 'pageurl'=>'someotherurl',
                 'notes'=>'anothernote'
                  ) 
          );

This array gets data sets added to and deleted from it by the user. The wp_list_table class I am using had its option set up the same way and I need to be able to edit those values later without having to delete the whole data set of the array I need to change and then recreating it.
I have tried presscoders example and ozh sample options solutions with no success. I am trying to update the name field in the array of arrays. Only reason I am using this array structure is because of the wp list table.

Read More

About to pull my hair out trying to get this to work.

Here is what I have tried:

add_action('admin_init', 'ozh_sampleoptions_init' );
add_action('admin_menu', 'ozh_sampleoptions_add_page');

// Init plugin options to white list our options
function ozh_sampleoptions_init(){
    register_setting( 'ozh_sampleoptions_options', 'fp_options', 'ozh_sampleoptions_validate' );
}

// Add menu page
function ozh_sampleoptions_add_page() {
    add_options_page('Ozh's Sample Options', 'Sample Options', 'manage_options', 'ozh_sampleoptions', 'ozh_sampleoptions_do_page');
}

// Draw the menu page itself
function ozh_sampleoptions_do_page() {
    ?>
    <div class="wrap">
        <h2>Ozh's Sample Options</h2>
        <form method="post" action="options.php">
            <?php settings_fields('ozh_sampleoptions_options'); ?>
            <?php $options = get_option('fp_options'); ?>
            <table class="form-table">

                <tr valign="top"><th scope="row">Tab Name</th>
                    <td><input type="text" name="ozh_sample[name]" size="75" value="<?php echo $options[0]['name']; ?>" /></td>
                </tr>
            </table>
            <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
            </p>
        </form>
    </div>
    <?php   
}

// Sanitize and validate input. Accepts an array, return a sanitized array.
function ozh_sampleoptions_validate($input) {

    // Say our option must be safe text with no HTML tags
    $input[0]['name'] =  wp_filter_nohtml_kses($input['name']);

    return $input;
}

Related posts

Leave a Reply

1 comment

  1. The Settings API generally expects data in the key => value form. I’m sure it is possible to save array data using the Settings API, but it somewhat circumvents the purpose of the API.

    If I’m understanding your question correctly, you’re trying to take a Plugin Option, and use the value of that option to update the wp_list_table class. The most straightforward approach might be to save some unique key as the Plugin option, and then cross-reference that unique key against a separate array-of-arrays to use to extend wp_list_table.

    In other words, build your wp_list_table values array, perhaps like so:

    <?php
    function plugin_slug_get_wp_list_table_data() {
        $data = array(
            'a' = array(
                'id'=> '1',
                'name'=> 'tom',
                'pageurl'=> 'someurl',
                'notes'=> 'someNotes'
            ),
            'b' = array(
                'id'=> '1',
                'name'=> 'tom',
                'pageurl'=> 'someurl',
                'notes'=> 'someNotes'
            ),
            'c' = array(
                'id'=> '1',
                'name'=> 'tom',
                'pageurl'=> 'someurl',
                'notes'=> 'someNotes'
            ),
            'd' = array(
                'id'=> '1',
                'name'=> 'tom',
                'pageurl'=> 'someurl',
                'notes'=> 'someNotes'
            ),
        );
        return $data;
    }
    ?>
    

    Then, save your Plugin option as 'a', 'b', 'c', or 'd'.

    Then, get your option:

    <?php
    $plugin_slug_options = get_option( 'plugin_slug_options' );
    $plugin_slug_wp_list_table_setting = $plugin_slug_options['wp_list_table_setting'];
    ?>
    

    Then, use the option setting to get the data with which to update wp_list_table:

    <?php
    $plugin_slug_wp_list_table_array = plugin_slug_get_wp_list_table_data();
    $plugin_slug_wp_list_table_data = $plugin_slug_wp_list_table_array[$plugin_slug_wp_list_table_setting];
    ?>
    

    (How you actually perform the wp_list_table update is up to you…)