Options

All plugins need some options. You may put your options model array in config/options.php:

config/options.php
<?php
 
if (!defined('ABSPATH')) {
    exit();
}
 
/*
|--------------------------------------------------------------------------
| Plugin options
|--------------------------------------------------------------------------
|
| Here is where you can insert the options model of your plugin.
| These options model will be stored in the WordPress options table
| (usually wp_options).
| You'll get these options by using $plugin->options property
|
*/
 
return [
 
  'version' => '1.0',
  'General' => [
    'option_1' => 'true',
    'option_2' => 'true',
    'option_3' => [
      'sub_option_of_3' => 'Hello'
    ],
    'option_4' => 'to delete',
    'option_5' => null,
  ],
 
  'Special' => [
    'Name' => 'James Kirk'
  ]
];

The options are subject to case sensitivity. So, take a look at the first key General. You should access it using $plugin->options->get('General.option_1'), for instance.

The plugin options are created upon the first activation of the plugin in the wp_options database table. The name of the option (option_name) will be the plugin slug. The options array will be converted to JSON and stored in option_value.

Get Options

To obtain an option from your model, simply call the get method:

echo $plugin->options->get( 'General.option_1');

Secondly, you may access the options like an array, since it implements PHP’s ArrayAccess interface:

echo $plugin->options[ 'General.option_1' ];

Of course, you may get a branch from your model by using:

echo $plugin->options->get( 'General.options_4');

That will return an array [ "sub_options_of_3" => 'Hello' ].

If the option doesn’t exist, you can define a default value:

echo $plugin->options->get( 'General.doNotExists', 'default');

As you can handle also the null value, you may use:

echo $plugin->options->get( 'General.option_5', 'default'); // will return 'default'

or

$value = $plugin->options->get( 'General.option_5');
if ( is_null( $value ) ) {
    echo 'default';
}

Update options

To update a single or branch option, simply call the set method:

$plugin->options->set( 'General.options_2', false );

or by using the array access:

$plugin->options[ 'General.options_2' ] = false;

The set method is also used to create a new option:

$plugin->options->set( 'Special.time', time() );

The value set may be a mixed value:

$plugin->options->set( 'Special.Name', [ 'John', 'Good' ] );

or by using the array access:

$plugin->options[ 'Special.Name' ] = [ 'Robin', 'Hood' ];

You may also use the update method to update a set of options:

$plugin->options->update(
    [
      'General' => [
        'option_4' => [
          'color'      => 'red',
          'background' => 'transparent',
          'images' => null
        ]
      ]
    ]
  );

Or insert a set of options:

$plugin->options->update(
    [
      'General' => [
        'option_5' => [
          'color'      => 'red',
          'background' => 'transparent',
          'images' => null
        ]
      ]
    ]
  );

Delete Options

To delete a single option, use:

$plugin->options->delete('General.option_4');

To delete all options, use:

$plugin->options->delete();

Reset to default options

To reset the options to the last version, please use:

$plugin->options->reset();

New version of options

When you release a new version of your plugin, you could add, remove, or delete some options. To do so, just edit your options array. When your plugin is updated and activated again, the new options will apply through a delta process.

Display options in a pretty format

You may display all plugin options in JSON by using echo $plugin->options. This feature is useful to inject your options like a JavaScript object:

add_action( 'wp_head', 'wp_head' );
 
public function wp_head()
{
    ?>
    <script>
      window.MyJavascriptGlobalObject = <?php echo MyPluginGlobalFunction()->options ?>;
    </script>
    <?php
}

Then you’ll find the following, in your HTML source page:

<script>
window.MyJavascriptGlobalObject = {
  "General": {
    "option_1": true,
    "option_2": true
  }
}
</script>

Get options as an array

If you want to obtain the options as a flat array, please use:

$result = MyPluginGlobalFunction()->options->toArray();

See Options in action

You can see the options in actions in the WP Kirk Demo plugin.