I stumbled upon one problem regarding too complicated(at least in my point of view) data structures stored in option field.
Right now I have an array of collections and within each other there is another sub-collection (as depicted below). In my opinion it looks too complicated, especially when one wants to store some value inside. More or less it would look like:
$collection = get_option('collection_setting');
$collection[1][elements][0][name] = "ChangedExample";
And this can get much worse when we would like to introduce another array inside of collection. Plus I think it can get quite sloppy if one uses like 100 entries per array.
My question is how could this be improved? Storing it into two different option fields - one for collection, second for sub-collections and then somehow trying to connect this within code? Or maybe this is perfectly OK?
Of course I assume that creating another table in DB is not the most elegant way.
Array
(
[0] => Array
(
[name] => collection_name
[speed] => 200
[id] => collection_name_1
[max_elements_per_page] => 13
[elements] => Array
(
[0] => Array
(
[id] => 1
[url] => http://www.example1.org
[name] => Example1
)
[1] => Array
(
[id] => 2
[url] => http://www.example2.org
[name] => Example2
)
[2] => Array
(
[id] => 3
[url] => http://www.example3.org
[name] => Example4
)
)
)
[1] => Array
(
[name] => collection_name_2
[speed] => 200
[id] => collection_name_2
[max_elements_per_page] => 13
[elements] => Array
(
[0] => Array
(
[id] => 1
[url] => http://www.example1.org
[name] => Example1
)
[1] => Array
(
[id] => 2
[url] => http://www.example2.org
[name] => Example2
)
[2] => Array
(
[id] => 3
[url] => http://www.example3.org
[name] => Example4
)
)
)
)
For example when I have setting page on which I want to edit only the first element of collection then with the moment I'm saving it, Wordpress overwrites whole option with only values submitted with form, erasing second element from collection (here $collection[1] is erased).
What is the way out? I'm thinking of using validate_options function( which is receiving data from setting page). I could retrieve collection_setting, modify it accordingly and returning modified collection_setting.
register_setting('collection-settings-group', 'collection_setting', 'validate_options');
But I think it's too much work and I'm sure there is some better solution for this.
wp_list_pluck
codex.wordpress.org/Function_Reference/wp_list_pluck