Bird
0
0

You need to save a site-wide setting that stores multiple user roles as an array. Which is the best way to store this using the Options API?

hard📝 Application Q8 of 15
Wordpress - Custom Fields and Meta Data
You need to save a site-wide setting that stores multiple user roles as an array. Which is the best way to store this using the Options API?
AConvert the array to JSON and save it with add_option('user_roles', json_encode($array)).
BSerialize the array and save it with update_option('user_roles', $array).
CSave each role as a separate option with add_option('user_role_1', 'admin'), etc.
DStore the array as a string separated by commas using update_option('user_roles', implode(',', $array)).
Step-by-Step Solution
Solution:
  1. Step 1: Understand Options API serialization

    WordPress automatically serializes arrays and objects when using update_option.
  2. Step 2: Use update_option with array

    Passing the array directly to update_option stores it correctly.
  3. Step 3: Other options drawbacks

    JSON encoding (B) is unnecessary; separate options (C) complicate retrieval; comma-separated string (D) loses data structure.
  4. Final Answer:

    Serialize the array and save it with update_option('user_roles', $array). -> Option B
  5. Quick Check:

    update_option auto-serializes arrays [OK]
Quick Trick: update_option auto-serializes arrays and objects [OK]
Common Mistakes:
  • Manually serializing or JSON encoding arrays unnecessarily
  • Storing array elements as separate options

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes