Bird
0
0

How can you ensure a site-wide option is only added if it does not already exist, and updated otherwise?

hard📝 Application Q9 of 15
Wordpress - Custom Fields and Meta Data
How can you ensure a site-wide option is only added if it does not already exist, and updated otherwise?
ADelete the option first, then add_option()
BCall add_option() then update_option() unconditionally
CCheck if option exists with get_option(), then add or update
DUse update_option() directly without add_option()
Step-by-Step Solution
Solution:
  1. Step 1: Understand update_option behavior

    update_option() adds the option if it does not exist, or updates it if it does.
  2. Step 2: Compare with other approaches

    Calling add_option() first risks failure if option exists; checking manually is extra work; deleting first is inefficient and risky.
  3. Final Answer:

    Use update_option() directly without add_option() -> Option D
  4. Quick Check:

    update_option adds or updates automatically [OK]
Quick Trick: update_option handles add or update automatically [OK]
Common Mistakes:
  • Calling add_option unnecessarily
  • Manually checking option existence
  • Deleting option before adding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes