custom plugin config options
I’ve recently had the request to add a custom config option to an existing plugin. Since I didn’t want to branch the plugin code for this and the options was not of general use I was a bit stuck.
I pondered options like writing custom SQL to clone an existing configuration entry, tracing SQL executed during plugin installation and some even more obscure things.
Then it hit me: the easiest way is, of course, to let the plugin itself do the work. Here are the steps to add custom configuration options to any ZenMagick plugin (of course, you still need to write the code to make use of that!).
- In your theme’s local.php (you might have to create that if it doesn’t exist) add the following lines:
global $zm_the_plugin;
$zm_the_plugin->addConfigValue('My custom option', 'myValue', '75', 'Custom plugin config value'); - Reload any storefront or admin page to execute the code
- Check the database for the new configuration value
- Either remove the code again, or wrap in conditional code, so it will automatically execute again if the plugin ever gets uninstalled/updated etc.
if(!defined(‘PLUGIN_REQUEST_ZM_THE_PLUGIN__MYVALUE’)) {
}
You can also create select boxes, etc, just have a look at what other plugins do in their install() method.
The value can be accessed like any other plugin value: $zm_the_plugin->get('myValue');
