Options

WordPress plugin options are divided into local options, available only on the current blog, and site options which are available on every blog in the network.

The SDK helps you automatically add options upon plugin activation, and inversely also removes them upon uninstall. The SDK also offers methods to retrieve, update and delete options. The reason you might want to use the SDK’s option handling methods is due to the name prefixing, in which the SDK adds the plugin name as a prefix to the option name, and because of the automatic registration and cleanup.

Local options

For options the your plugin will always use, you must first declare them in a method.

public function local_options()
{
  return array_merge( [
    'load_users' => true,		// Describe this option
    'users_to_load' => [ 1, 2, 3 ],	// Arrays are allowed
  ], parent::local_options() );
}

This method declares an array of option key => value pairs, and then merges the array with the options array from the parent class, which could be the SDK WordPress base class or perhaps a general superclass you have as a shim just before the base class.

Now the SDK knows which options to automatically insert into the options table upon plugin activation. If your plugin uses the admin_uninstall method to uninstall itself, the options will be automatically deleted.

The following methods are available to manipulate local options:

  • delete_local_option( $name )
  • get_local_option( $name, $default )
  • update_local_option( $name, $value )

get_local_option has several defaults that are used if the option is not found:

  1. $default as specified when calling the method
  2. The value from the local_options() array
  3. false

The local options prefix

Local options are prefixed with a string, that is returned from the get_local_option_prefix() method. The default value is the class name.

If you have WP_DEBUG enabled and find that the SDK complains of the option name being too long, override the above method to return something shorter.

Site options

Site options are reachable from all blogs. To manipulate site options, replace the word local in the text above with site.

  • site_options()
  • delete_site_option
  • get_site_option
  • etc…

 

Leave a Reply

Your email address will not be published. Required fields are marked *