You can use the wordpress Options API without the Settings API (but you can’t use Settings API without Options API).

The Options API is great for storing general settings for you plugging, site, etc.

Store a value or array

  $MyStoredSetting = "something...";

  update_option('MY_UNIQUE_SETTING_NAME', $MyStoredSetting , True);       //If the option does not exist it will be created.  autoload=True(load the option when WordPress starts up)

Read stored value or array

You don’t have to stored a value first, get_option() will return False if none found.

$MyStoredSetting can be a value or an array, get_option() will serialise it if necessary

  
  if (!$MyStoredSetting = get_option('MY_UNIQUE_SETTING_NAME'))
    $MyStoredSetting = "";

Delete a stored setting

  delete_option('MY_UNIQUE_SETTING_NAME');

Add a new stored setting

You can use this if you wish, but update_option() will create a new setting if it doesn’t already exist

  add_option('MY_UNIQUE_SETTING_NAME', 'options_value', True);       //autoload=True(load the option when WordPress starts up)

Reading and writing an on/off True/False value example

Read it
  if (!$MyOptionSettingVariable = get_option('MyOptionSettingName'))
    $MyOptionSettingVariable = False;

  //$MyOptionSettingVariable is now set as True or False
Write it
  $MyOptionSettingVariable = False;
  $MyOptionSettingVariable = True;

  update_option('MyOptionSettingName', $MyOptionSettingVariable, True);     //If the option does not exist it will be created.  autoload=True(load the option when WordPress starts up)

Reading and writing a DateTime value example

Read it
  $SecondsAgo = -1;
  if ($MyOptionSettingName = get_option('MyOptionSettingName'))
    $SecondsAgo = strtotime(date("Y-m-d H:i:s")) - strtotime($MyOptionSettingName);
Write it
  //Store the current DateTime
  update_option('MyOptionSettingName', date('Y-m-d H:i:s'), True);     //If the option does not exist it will be created.  autoload=True(load the option when WordPress starts up)
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

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