Step 4. Adding Configuration To Your Plugin

There are two types of configuration you can use within your plugin:

The basic rule of thumb is use "Config Values" unless you are 100% confident in allowing users to set the values of the option.

Creating Config Values

Creating hardcoded config values is as simple as creating a key/value array within the conf/config.php file:

<?php
return array(
    'my_config_key' => 'my_config_value',
    'my_other_config_key' => 'my_other_config_value',
);

Creating Options

Options are generated during the installation process, so please refer to he Creating An Installer And Uninstaller article for more information.

Running Custom Rules When An Option Is Updated

If you have special considerations which need to be accounted for when someone updates an option, you can handle those special rules by creating an "option file" in the "/options" folder of your plugin. You will need to name the option file in accordance with this standard:

So for example, in the Twilio plugin, we have a custom option named "apiphonenumber" which required that we append a "+" to the start of the option. To handle this, we created a file named pg_twilio_apiphonenumber.php:

<?php
$value = '+' . ltrim($value, '+');
$this->update_option($key, $value);

Notice that we MUST invoke the update_option($key, $value) function to make this work. And since this is included directly into a class, you can use $this-> despite the file not containing a class of its own.

You can see this example here. Note that $key and $value are automatically available in your option file and represent the option name and new value submitted by the user.

Accessing Config Values and Options

Config values are accessible using the plugin::getGlobal($key)** method, while options are accessible using the *plugin::getOption($key) method.

<?php
class myCustomPluginClass extends pluginLoader {
    public function configValueExample()
    {
        // The entire global array:
        var_dump($this->plugin->global);

        // A single config value:
        var_dump($this->plugin->getGlobal('my_config_key'));
    }
    public function optionExample()
    {
        // The entire option array:
        var_dump($this->plugin->options);

        // A single option value:
        var_dump($this->plugin->getOption('my_option_key'));
    }
}

Special Config Values/Local Database Connections

The plugin::connectLocal() method is a convenient shortcut allowing you to connect to your plugin's database, if any. In order for this to work, you will need set up the following config values:

<?php

return array(
    'mysql_host' => 'localhost',
    'mysql_db' => 'db_name',
    'mysql_user' => 'db_username',
    'mysql_pass' => 'db_password',
    'mysql_prefix' => 'dbprefix_',
);

Now you can flip between the plugin's database and Zenbership's database using the plugin::connectLocal() and plugin::connectZen() methods, available in your custom classes which extend the pluginLoader as $this->plugin->connectZen() and $this->plugin->connectLocal().

Last modified 4 months ago.