Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have data in the wp_options table currently stored as a multi-dimensional array (profile_element_order):

a:12:{s:17:"img_base64_enable";s:1:"1";s:25:"moulding_combination_page";s:0:"";s:24:"moulding_collection_page";s:0:"";s:25:"idea_gallery_thumb_height";s:3:"200";s:24:"idea_gallery_thumb_width";s:3:"200";s:23:"collection_thumb_height";s:3:"200";s:22:"collection_thumb_width";s:3:"200";s:20:"profile_item_columns";s:1:"4";s:17:"idea_item_columns";s:1:"2";s:24:"collections_item_columns";s:1:"2";s:25:"combinations_item_columns";s:1:"4";s:21:"profile_element_order";a:5:{i:0;s:8:"Option 1";i:1;s:8:"Option 2";i:2;s:8:"Option 3";i:3;s:8:"Option 4";i:4;s:8:"Option 5";}}

What I'm trying to accomplish is update the profile_element_order option (within those options). Here's how everything looks so far:

function psort_save_order() {

    global $mouldings_options;

    $list = $mouldings_options['profile_element_order'];
    $new_order = $_POST['list_items'];
    $new_list = array();

    // update order
    foreach($new_order as $v) {
        if(isset($list[$v])) {
            $new_list[$v] = $list[$v];
        }
    }

    // save the new order
    update_option('profile_element_order', $new_list);

    die();
}
add_action('wp_ajax_psort_update_order', 'psort_save_order');

The data is correctly posting to the DB table (as I can see some of my failed attempts as new option entries, like mouldings_settings->profile_element_order) -- I'm just having a hard time figuring out the update_option() syntax for just that specific option. I've tried things like (bearing in mind `mouldings_settings is the actual option name):

mouldings_settings['profile_element_order']
$mouldings_options['profile_element_order']
profile_element_order

but no dice at the moment. Any pointers would be greatly appreciated! Thanks!

Update This is what I have now -- ajax action saves fine, but when I save the plugin options, it duplicates the options in the database and throws up the same error as before:

a:17:{s:17:"img_base64_enable";s:1:"1";s:25:"moulding_combination_page";s:0:"";s:24:"moulding_collection_page";s:0:"";s:25:"idea_gallery_thumb_height";s:3:"200";s:24:"idea_gallery_thumb_width";s:3:"200";s:23:"collection_thumb_height";s:3:"200";s:22:"collection_thumb_width";s:3:"200";s:20:"profile_item_columns";s:1:"4";s:17:"idea_item_columns";s:1:"2";s:24:"collections_item_columns";s:1:"2";s:25:"combinations_item_columns";s:1:"4";s:21:"profile_element_order";a:5:{i:4;s:8:"Option 5";i:0;s:8:"Option 1";i:1;s:8:"Option 2";i:3;s:8:"Option 4";i:2;s:8:"Option 3";}i:0;s:8:"Option 5";i:1;s:8:"Option 1";i:2;s:8:"Option 2";i:3;s:8:"Option 4";i:4;s:8:"Option 3";}

Function:

function psort_save_order() {

    global $mouldings_options;

    $list = $mouldings_options['profile_element_order'];
    $new_order = $_POST['list_items'];
    $new_list = array();

    // update order
    foreach($new_order as $v) {
        if(isset($list[$v])) {
            $new_list[$v] = $list[$v];
        }
    }

    $mouldings_options['profile_element_order'] = $new_list;
    $mouldings_options = array_merge($mouldings_options,$mouldings_options['profile_element_order']);

    // save the new order
    update_option('mouldings_settings', $mouldings_options);

    die();
}
add_action('wp_ajax_psort_update_order', 'psort_save_order');
share|improve this question

1 Answer 1

up vote 11 down vote accepted

As far WordPress is concerned - your multi-dimensional array is one option.

To update just part of the multi-dimensional array its necessary to retrieve the entire array, alter it accordingly and then update the entire array.

Suppose your multi-dimensional array is as follows:

my_options = array(
  'option_a'=>'value_a',
  'option_b'=>'value_b',
  'inner_array'=>array(
       'foo' => 'bar',
       'hello' => 'world',
   ),
  'option_c'=>'value_c'
)

And suppose you want to update the value of the 'hello' option from 'world' to 'moon'

//Get entire array
$my_options = get_option('my_options');

//Alter the options array appropriately
$my_options['inner_array']['hello'] = 'moon';

//Update entire array
update_option('my_options', $my_options);
share|improve this answer
    
Hi Steven - I've updated my original question with what I have now and while it does work with Ajax (saves the position when I refresh) once I save the plugin settings and refresh, the options in the DB table get a little messy (looks duplicated) which is throwin an error -- am I still approaching that wrong? Thanks. –  Zach May 11 '12 at 12:55
    
Are you saying that the row itself gets duplicated? Or your options appear duplicated inside the row? Try deleting the option and trying again - it could be that you're simply keeping the duplicates from your previous attempts. –  Stephen Harris May 11 '12 at 13:38
    
Hi Stephen - made a pastebin of my steps to reproduce the issue: pastebin.com/YHg1i7HR Thanks! –  Zach May 11 '12 at 14:04
    
Try removing your array_merge. That's causing the duplication. You are merging an array with a sub-array (causing the sub-array to duplicated). –  Stephen Harris May 11 '12 at 14:11
    
Hi Stephen - I guess I'm just unsure of how to write that then - I just have $mouldings_options['profile_element_order'] = $new_list;update_option('mouldings_settings', mouldings_options); now which does not have the duplication (and writes to the DB correctly which is good) -- but on page-save (plugin settings save), the plugin setting is removed from the database still (as the last part of that pastebin does). –  Zach May 11 '12 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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