I have run into a slight issue when creating a WordPress plugin. The plugin is class based and the problem appears to be with the foreach call within a function. The foreach call is iterating through an array and adding options to WordPress using the options api. Any help getting this to work correctly would be appreciated.
The Array
$settings = array();
$settings['version'] = '0.1';
$settings['release'] = 'development';
$settings['license_accepted'] = 'false';
The Function
public function settings($action) {
$supported_actions = array('install', 'update', 'uninstall');
if (in_array($action, $supported_actions) == true) {
foreach($settings as $setting => $value) {
$current = 'plugin_'.$setting;
if ($action == 'install') {
add_option($current, $value, null, true);
}
if ($action == 'update') {
update_option($current, $value, null, true);
}
if ($action == 'uninstall') {
delete_option($current);
}
}
} else {
return false;
}
}
The Problem
Warning: Invalid argument supplied for foreach.
You need to bring the
$settingsvariable inside the function scope. You can do that by using a argument:Or use Dependency Injection (I prefer), but I think that is to difficult for you and the plugin.