Bird
Raised Fist0
Wordpressframework~20 mins

Options API for site-wide settings in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Options API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this code output when retrieving an option?
Consider this WordPress code snippet that retrieves a site-wide setting using the Options API. What will be the output of echo $site_name; if the option 'site_name' was previously saved as 'My Cool Site'?
Wordpress
<?php
$site_name = get_option('site_name', 'Default Site');
echo $site_name;
?>
AMy Cool Site
BDefault Site
Csite_name
DNULL
Attempts:
2 left
💡 Hint
Think about what get_option returns when the option exists.
📝 Syntax
intermediate
2:00remaining
Which option correctly updates a site-wide option?
You want to update the option 'homepage_message' to 'Welcome!' using the Options API. Which code snippet correctly performs this update?
Aupdate_option('homepage_message', 'Welcome!');
Bsave_option('homepage_message', 'Welcome!');
Cset_option('homepage_message', 'Welcome!');
Dput_option('homepage_message', 'Welcome!');
Attempts:
2 left
💡 Hint
Check the official function name for updating options.
🔧 Debug
advanced
2:00remaining
Why does this code fail to save the option?
This code tries to save a new option but it does not work as expected. What is the reason?
Wordpress
<?php
add_option('footer_text', 'Thank you for visiting!');
add_option('footer_text', 'New footer text');
?>
Aadd_option must be called inside a function hooked to init
Badd_option requires a third parameter for autoload, missing here
Cadd_option cannot save strings, only arrays
Dadd_option only adds if the option does not exist; second call fails silently
Attempts:
2 left
💡 Hint
Think about what happens if you try to add an option that already exists.
❓ state_output
advanced
2:00remaining
What is the value of $result after this code runs?
Given this code snippet, what will be the value of $result?
Wordpress
<?php
update_option('color_scheme', 'dark');
$result = get_option('color_scheme', 'light');
?>
ANULL
B'light'
C'dark'
Dfalse
Attempts:
2 left
💡 Hint
Remember what update_option does and what get_option returns.
🧠 Conceptual
expert
2:00remaining
Which statement about the Options API is TRUE?
Select the correct statement about the WordPress Options API for site-wide settings.
AOptions API can only store numeric values, not strings or arrays.
BOptions API stores data in the wp_options table and is best for small pieces of data.
COptions API requires manual database queries to retrieve options.
DOptions API automatically encrypts all stored options for security.
Attempts:
2 left
💡 Hint
Think about where WordPress stores options and what kind of data it handles.

Practice

(1/5)
1. What is the main purpose of the WordPress Options API?
easy
A. To handle user login and authentication
B. To create new posts automatically
C. To store and manage site-wide settings easily
D. To style the website with CSS

Solution

  1. Step 1: Understand the role of Options API

    The Options API is designed to store settings that affect the whole site, like site title or custom settings.
  2. Step 2: Compare with other options

    Creating posts, user login, and styling are handled by other parts of WordPress, not the Options API.
  3. Final Answer:

    To store and manage site-wide settings easily -> Option C
  4. Quick Check:

    Options API = site-wide settings [OK]
Hint: Options API is for site settings, not content or style [OK]
Common Mistakes:
  • Confusing Options API with post creation
  • Thinking it manages user authentication
  • Assuming it handles CSS styling
2. Which function is used to retrieve a stored option value in WordPress?
easy
A. add_option()
B. get_option()
C. update_option()
D. delete_option()

Solution

  1. Step 1: Identify function purposes

    add_option() adds a new option, update_option() changes it, delete_option() removes it, and get_option() retrieves the value.
  2. Step 2: Match retrieval action

    Since we want to get or read the stored value, get_option() is the correct function.
  3. Final Answer:

    get_option() -> Option B
  4. Quick Check:

    Retrieve option = get_option() [OK]
Hint: Get means retrieve, so get_option() fetches values [OK]
Common Mistakes:
  • Using add_option() to get values
  • Confusing update_option() with retrieval
  • Trying to delete instead of get
3. What will be the output of this code snippet?
add_option('site_color', 'blue');
update_option('site_color', 'green');
echo get_option('site_color');
medium
A. blue
B. null
C. site_color
D. green

Solution

  1. Step 1: Understand add_option and update_option

    add_option sets 'site_color' to 'blue'. Then update_option changes it to 'green'.
  2. Step 2: Retrieve current value

    get_option('site_color') returns the latest value, which is 'green'.
  3. Final Answer:

    green -> Option D
  4. Quick Check:

    Updated option value = green [OK]
Hint: Update_option overwrites add_option value [OK]
Common Mistakes:
  • Assuming add_option value stays after update
  • Expecting null if updated
  • Confusing option name with value
4. Identify the error in this code snippet:
update_option('header_text', 'Welcome!');
add_option('header_text', 'Hello!');
echo get_option('header_text');
medium
A. No error, code works fine
B. get_option cannot retrieve 'header_text'
C. update_option cannot update existing options
D. add_option should be called before update_option

Solution

  1. Step 1: Understand function order

    add_option adds a new option only if it doesn't exist. update_option updates existing or adds if missing.
  2. Step 2: Analyze code order

    Calling update_option first works fine because it adds or updates the option. add_option after will fail to add since the option exists, but this does not cause an error.
  3. Step 3: Correct usage

    While add_option before update_option is typical, the code runs without error.
  4. Final Answer:

    No error, code works fine -> Option A
  5. Quick Check:

    update_option can add option; add_option after does nothing [OK]
Hint: update_option can add option; add_option after does nothing [OK]
Common Mistakes:
  • Calling add_option after update_option
  • Thinking update_option can't add new options
  • Assuming get_option fails here
5. You want to store a site-wide setting that controls whether comments are enabled. Which approach correctly uses the Options API to toggle this setting on and then retrieve it?
hard
A. update_option('comments_enabled', true); echo get_option('comments_enabled');
B. add_option('comments_enabled', true); update_option('comments_enabled', false); echo get_option('comments_enabled');
C. add_option('comments_enabled', true); echo get_option('comments_enabled'); update_option('comments_enabled', false);
D. delete_option('comments_enabled'); add_option('comments_enabled', true); echo get_option('comments_enabled');

Solution

  1. Step 1: Understand update_option behavior

    update_option adds the option if it doesn't exist, so calling it first with true sets the value.
  2. Step 2: Retrieve current value

    Calling get_option right after returns true, showing comments enabled.
  3. Step 3: Toggle setting

    To toggle off, you would call update_option('comments_enabled', false) later. update_option('comments_enabled', true); echo get_option('comments_enabled'); shows correct initial set and get.
  4. Final Answer:

    update_option('comments_enabled', true); echo get_option('comments_enabled'); -> Option A
  5. Quick Check:

    update_option adds or updates, then get_option reads [OK]
Hint: update_option can add new option; no need to add_option first [OK]
Common Mistakes:
  • Calling add_option then update_option without retrieval
  • Deleting option unnecessarily
  • Expecting add_option to update existing option
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard