The problem: to change a widget's options with a custom function in functions.php, when there are no useful hooks, and where there are multiple occurrences of the same widget (multiwidget).
This would involve:
Retrieving the options from "widget_some-widget" in the "wp_options" table. (The returned option containing a string of settings (in serialized form) repeated several times corresponding to several unique occurrences of the same widget.)
Changing the value of say the third occurrence of an option instance called
some_name
, from "old-value" to "new-value", and another calledsome_other_name
, from "some-old-value" to "some-new-value". (It may be that each occurrence of the widget has its own id within the serialized options string that could be recognised somehow?)Updating the database with the altered options
i.e. something along these lines -
function change_widget_configuration() {
$options = get_option('widget_some-widget');
// [code to parse $options string to ensure the correct occurrences of 'some_name' and 'some_other_name' have their values changed]
/* third-occurrence of */ $options['some_name'] = 'new value';
/* third-occurrence of */ $options['some_other_name'] = 'new value';
update_option('widget_some-widget', $options);
}
The widget's serialized options string might look something like this (minus the ellipsis) -
i:3;a:20:{s:5:"title";s:8:"My Title";s:9:"some_name";s:11:"Bit of text";s:15:"some_other_name";s:2:"17";
[...] s:14:"more_from_this";s:29:"More articles on this subject";}s:12:"_multiwidget";i:1;}
What kind of approach can be taken (with the commented-out code above) to solve this problem? *
*This would be very useful for controlling the output of third-party widgets (that do not have any useful hooks) without needing to alter their code, or repeating large chunks of it in the functions.php