0
0
Wordpressframework~10 mins

Options API for site-wide settings in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Options API for site-wide settings
Define option name and value
Use add_option() to save
Option stored in database
Use get_option() to retrieve
Use update_option() to change value
Use delete_option() to remove
End
This flow shows how WordPress Options API stores, retrieves, updates, and deletes site-wide settings in the database.
Execution Sample
Wordpress
<?php
add_option('site_color', 'blue');
echo get_option('site_color');
update_option('site_color', 'red');
echo get_option('site_color');
delete_option('site_color');
?>
This code saves a color option, reads it, updates it, reads again, then deletes it.
Execution Table
StepFunction CalledOption NameValue BeforeActionValue AfterOutput
1add_optionsite_colornoneAdd 'blue'bluenone
2get_optionsite_colorblueRetrieve valueblueblue
3update_optionsite_colorblueUpdate to 'red'rednone
4get_optionsite_colorredRetrieve updated valueredred
5delete_optionsite_colorredRemove optionnonenone
6get_optionsite_colornoneTry retrieve deletednonefalse
💡 Option deleted at step 5, so retrieval at step 6 returns false.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 6
site_color option valuenonebluerednonenone
Key Moments - 3 Insights
Why does get_option return false after delete_option?
Because delete_option removes the option from the database, so get_option finds nothing and returns false as shown in step 6 of the execution_table.
What happens if add_option is called with an existing option name?
add_option will not overwrite an existing option. It only adds if the option does not exist, so if the option exists, the value stays unchanged as shown by the initial add_option in step 1.
How does update_option differ from add_option?
update_option changes the value if the option exists or adds it if missing. add_option only adds if missing. This is why update_option in step 3 changes 'blue' to 'red'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of get_option at step 4?
A"blue"
Bfalse
C"red"
Dnone
💡 Hint
Check the Output column at step 4 in the execution_table.
At which step does the option value change from 'blue' to 'red'?
AStep 1
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the Value After column in the execution_table for the update_option call.
If we call add_option('site_color', 'green') after step 5, what will get_option('site_color') return?
A"green"
B"red"
Cfalse
Dnone
💡 Hint
add_option adds a new option only if it does not exist, so after deletion at step 5, it will add 'green'.
Concept Snapshot
Options API stores site-wide settings in WordPress.
Use add_option(name, value) to save new settings.
Use get_option(name) to read settings.
Use update_option(name, value) to change existing settings.
Use delete_option(name) to remove settings.
get_option returns false if option missing.
Full Transcript
This visual trace shows how WordPress Options API manages site-wide settings. First, add_option saves a new option 'site_color' with value 'blue'. Then get_option reads it and outputs 'blue'. update_option changes the value to 'red'. Another get_option outputs 'red'. delete_option removes the option. Finally, get_option returns false because the option no longer exists. This step-by-step shows how options are stored, retrieved, updated, and deleted in WordPress.