Options: Adding global options - dotherightthing/wpdtrt-plugin-boilerplate GitHub Wiki

Summary

DTRT WordPress Plugin Boilerplate supports user-configurable global options.

Status

  • Stable @ 1.3.2

Usage

Register an option

The option array describes an input field which will be used to set a value on the plugin options page in WordPress admin.

Input field types are self-documented in ./views/form-element-*.php.

If you would rather author this value from a widget or shortcode, please see Add a shortcode or widget option.

Example from ./wpdtrt-exif.php

$plugin_options = array(
  'google_static_maps_api_key' => array(
    'type' => 'text',
    'label' => __('Google Static Maps API Key', 'wpdtrt-exif'),
    'size' => 50,
    'tip' => __('https://developers.google.com/maps/documentation/static-maps/ > GET A KEY', 'wpdtrt-exif')
  )
);

Get the value of an option

This is a two step process: get all the plugin options, then get the specific key.

Example from ./src/legacy/attachment-field-gps.php

// the global needs to be referenced when calling plugin methods from a template
global $wpdtrt_exif_plugin;

$plugin_options = $wpdtrt_exif_plugin->get_plugin_options();
$google_static_maps_api_key = $plugin_options['google_static_maps_api_key'];

Update the value of an option

// get current options
$plugin_options = $this->get_plugin_options();

// add a new option, or update an existing one
$plugin_options['option_to_update'] = 'baz';

// save the update
$this->set_plugin_options( $plugin_options );