I’m writing a course information plugin. Most course info is related to
to a post (a custom post type of ‘course’), so I have put the majority of info in the postmeta table. But some info isn’t related to any of the posts – venue information for example. I’m using the options table to store that info.

I’ve added a set of options pages, and am using the settings API to edit basic information. The problem is that some of the info is dynamic and more complicated:

I have a venue page where I need to support adding new venues, and editing them. Each venue has a title, 3 address lines and a postcode. An example of the array of venues is:

array(
    1 => array(
                'venue_title' = 'Edge Hill University, Ormskirk',
                'address_1'   = '20 Edge Lane',
                'address_2'   = 'Ormskirk',
                'address_3'   = 'Lancashire',
                'postcode'    = 'L12 6EA'
         ),
    2 => array(
                'venue_title' = 'Edge Hill University, Aintree',
                'address_1'   = '12 Aintree Close',
                'address_2'   = 'Aintree',
                'address_3'   = 'Liverpool',
                'postcode'    = 'L48 6PQ'
         ),
    3 => array ( ...etc other venues)

);

So to make any changes I use text fields. These get handled by the settings api. The name attribute and content are set dynamically from a $_GET variable.

<input type="text" name="venues[<?php echo $venue_id; ?>][venue_title]" value="<?php echo $venues[$venue_id]['venue_title']; ?>" />

which would output as

<input type="text" name="venues[2][venue_title]" value="<?php echo $venues[2]['venue_title']; ?>" />

I only have one set of these fields registered with the settings api, which is where i think the problem is, as when I change one, it removes all the other ones and just inserts the new one.

So in my mind the code above should update just the $venues[2][venue_title], but it overwrites all venues. I think because there is only one set of fields registered it will only update as if there is one set of fields. Does anyone know a simple way to make this work?

Thanks

1 Answer
1

Look at your validation function (the one referenced by your register_setting call). Whatever it returns replaces the entire setting in the database. So if it’s not getting the setting and then altering it and returning the entire setting, you’ll lose the data that you didn’t have it return.

Leave a Reply

Your email address will not be published. Required fields are marked *