{"id":3537,"date":"2021-01-13T14:43:52","date_gmt":"2021-01-13T14:43:52","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3537"},"modified":"2026-04-01T21:44:02","modified_gmt":"2026-04-01T20:44:02","slug":"options-api","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/database\/built-in-db-functions\/options-api","title":{"rendered":"Options API"},"content":{"rendered":"\n<p>You can use the wordpress Options API without the Settings API (but you can&#8217;t use Settings API without Options API).<\/p>\n\n\n\n<p>The Options API is great for storing general settings for you plugging, site, etc.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Store a value or array<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyStoredSetting = \"something...\";\n\n  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)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read stored value or array<\/h4>\n\n\n\n<p>You don&#8217;t have to stored a value first, get_option() will return False if none found.<\/p>\n\n\n\n<p>$MyStoredSetting can be a value or an array, get_option() will serialise it if necessary<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \n  $MyStoredSetting = get_option('MY_UNIQUE_SETTING_NAME', \"\");    \/\/The 2nd argument is the default value to use if the option does not exist\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete a stored setting<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  delete_option('MY_UNIQUE_SETTING_NAME');<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add a new stored setting<\/h4>\n\n\n\n<p>You can use this if you wish, but update_option() will create a new setting if it doesn&#8217;t already exist<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  add_option('MY_UNIQUE_SETTING_NAME', 'options_value', True);       \/\/autoload=True(load the option when WordPress starts up)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reading and writing an on\/off True\/False value example<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Read it<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (!$MyOptionSettingVariable = get_option('MyOptionSettingName'))\n    $MyOptionSettingVariable = False;\n\n  \/\/$MyOptionSettingVariable is now set as True or False<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Write it<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyOptionSettingVariable = False;\n  $MyOptionSettingVariable = True;\n\n  update_option('MyOptionSettingName', $MyOptionSettingVariable, True);     \/\/If the option does not exist it will be created.  autoload=True(load the option when WordPress starts up)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Reading and writing a DateTime value example<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Read it<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $SecondsAgo = -1;\n  if ($MyOptionSettingName = get_option('MyOptionSettingName'))\n    $SecondsAgo = strtotime(gmdate(\"Y-m-d H:i:s\")) - strtotime($MyOptionSettingName);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Write it<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Store the current DateTime\n  update_option('MyOptionSettingName', gmdate('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)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can use the wordpress Options API without the Settings API (but you can&#8217;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 Read stored value or array You don&#8217;t have to stored a value first, get_option() will return [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[296,353],"tags":[],"class_list":["post-3537","post","type-post","status-publish","format-standard","hentry","category-built-in-db-functions","category-settongs"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3537"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3537\/revisions"}],"predecessor-version":[{"id":5333,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3537\/revisions\/5333"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}