2

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.

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;
}
5
  • Can you provide a bit more context? What data are you trying to save as an array, and why do those data need to be saved as an array? Commented Feb 23, 2012 at 19:35
  • I am saving an, id, name, url, and personal notes in an array of arrays as shown above. The option is being saved in the wp_options table, when created by another part of my plugin. I need an options page to enable the user to change the data at will, but i can't get the settings API to save the changes. The option will store multiple arrays with data for different pages and urls.
    – kaira
    Commented Feb 23, 2012 at 19:42
  • I understand that, but the important part of the question is: why do these data need to be in their own array? Commented Feb 23, 2012 at 19:44
  • because that is how the wp_list_table class stores them and I am using that class to add new data set arrays to the option table and they need to match the same structure.
    – kaira
    Commented Feb 23, 2012 at 19:47
  • If I can figure out how to update one field then the rest will be easy
    – kaira
    Commented Feb 23, 2012 at 19:52

1 Answer 1

2

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...)

1
  • Admittedly, a unique key would have been easier to deal with instead of an array of indexed arrays, but the wp_list_table class is not the only part of the plugin that uses that data. the data array I have shown here is one of 3, all referenced by the id field.
    – kaira
    Commented Feb 23, 2012 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.