Bird
Raised Fist0
Wordpressframework~20 mins

Reading and writing settings in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
WordPress Settings Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this WordPress settings retrieval code?
Consider this code snippet that reads a setting from the WordPress options table:

 $value = get_option('my_plugin_setting', 'default');
echo $value;

Assuming the option 'my_plugin_setting' is not set in the database, what will be printed?
Wordpress
$value = get_option('my_plugin_setting', 'default');
echo $value;
APrints 'default'
BCauses a fatal error
CPrints NULL
DPrints an empty string
Attempts:
2 left
💡 Hint
Check what the second parameter of get_option does.
state_output
intermediate
2:00remaining
What is the value of the option after this code runs?
Given this code snippet:

 update_option('my_plugin_setting', 'blue');
update_option('my_plugin_setting', 'red');
$color = get_option('my_plugin_setting');
echo $color;

What will be the output?
Wordpress
update_option('my_plugin_setting', 'blue');
update_option('my_plugin_setting', 'red');
$color = get_option('my_plugin_setting');
echo $color;
Ablue
Bred
CNULL
DAn error is thrown
Attempts:
2 left
💡 Hint
Think about what update_option does when called twice with the same key.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error when saving settings?
Which of the following code snippets will cause a syntax error when trying to save a setting in WordPress?
Aupdate_option('my_setting', array('a' => 1));
Bupdate_option('my_setting', 'value');
Cupdate_option('my_setting' 'value');
Dupdate_option('my_setting', 123);
Attempts:
2 left
💡 Hint
Look carefully at the commas between function arguments.
🔧 Debug
advanced
2:00remaining
Why does this code fail to save the setting?
This code tries to save a setting but it does not persist:

 $setting = 'dark_mode';
update_option($setting, true);
if(get_option('darkmode')) {
echo 'Enabled';
} else {
echo 'Disabled';
}

What is the reason?
Wordpress
$setting = 'dark_mode';
update_option($setting, true);
if(get_option('darkmode')) {
  echo 'Enabled';
} else {
  echo 'Disabled';
}
Aget_option requires a default value parameter
Bupdate_option cannot save boolean values
CThe variable $setting is not defined
DThe option name is inconsistent between update_option and get_option
Attempts:
2 left
💡 Hint
Check the option names used in update_option and get_option calls.
🧠 Conceptual
expert
3:00remaining
Which option best describes how WordPress stores settings saved with update_option?
When you use update_option('my_setting', $value) in WordPress, how is the data stored internally?
AIt stores the value serialized in the wp_options table under the option_name 'my_setting'
BIt stores the value in a transient cache that expires after 1 hour
CIt stores the value as plain text in a custom file in the wp-content folder
DIt stores the value encrypted in the usermeta table
Attempts:
2 left
💡 Hint
Think about where WordPress stores options by default.

Practice

(1/5)
1. Which WordPress function is used to read a saved setting from the database?
easy
A. get_option
B. save_option
C. set_option
D. read_option

Solution

  1. Step 1: Understand the purpose of each function

    get_option is the WordPress function designed to retrieve saved settings. The others are not valid WordPress functions.
  2. Step 2: Confirm the correct function for reading settings

    Since get_option reads the saved option value, it is the correct choice.
  3. Final Answer:

    get_option -> Option A
  4. Quick Check:

    Read settings = get_option [OK]
Hint: Remember: get_option reads, update_option writes [OK]
Common Mistakes:
  • Confusing get_option with update_option
  • Using non-existent functions like save_option
  • Thinking set_option reads settings
2. Which of the following is the correct syntax to update a setting named 'site_color' to 'blue'?
easy
A. update_option('site_color', 'blue');
B. update_option('site_color' => 'blue');
C. get_option('site_color', 'blue');
D. set_option('site_color', 'blue');

Solution

  1. Step 1: Identify the correct function and syntax for updating

    The function to update a setting is update_option, which takes two parameters: the option name and the new value.
  2. Step 2: Check the syntax of each option

    update_option('site_color', 'blue'); correctly uses update_option('site_color', 'blue');. update_option('site_color' => 'blue'); uses an incorrect array syntax. get_option('site_color', 'blue'); uses get_option which reads, not writes. set_option('site_color', 'blue'); uses a non-existent function.
  3. Final Answer:

    update_option('site_color', 'blue'); -> Option A
  4. Quick Check:

    Update syntax = update_option(name, value) [OK]
Hint: update_option needs two arguments: name and value [OK]
Common Mistakes:
  • Using get_option to update values
  • Passing arguments as an array instead of separate parameters
  • Using non-existent set_option function
3. What will be the output of this code snippet?
$color = get_option('site_color', 'red');
echo $color;

Assuming 'site_color' is not set in the database.
medium
A. site_color
B. null
C. '' (empty string)
D. red

Solution

  1. Step 1: Understand get_option default value behavior

    If the option 'site_color' does not exist, get_option returns the default value provided as the second argument, here 'red'.
  2. Step 2: Determine the output of echo

    Since 'site_color' is not set, $color will be 'red', so echo outputs 'red'.
  3. Final Answer:

    red -> Option D
  4. Quick Check:

    Missing option returns default value [OK]
Hint: get_option returns default if option missing [OK]
Common Mistakes:
  • Assuming get_option returns empty string if missing
  • Confusing option name with value
  • Expecting null instead of default
4. Identify the error in this code snippet:
update_option('background_color');
medium
A. Function name should be get_option to update
B. update_option cannot update 'background_color'
C. Missing the new value parameter in update_option
D. update_option requires three parameters

Solution

  1. Step 1: Check update_option function parameters

    update_option requires two parameters: the option name and the new value to set.
  2. Step 2: Identify the missing parameter

    The code only passes the option name, missing the new value, causing an error.
  3. Final Answer:

    Missing the new value parameter in update_option -> Option C
  4. Quick Check:

    update_option needs name and value [OK]
Hint: update_option always needs two arguments [OK]
Common Mistakes:
  • Passing only one argument to update_option
  • Confusing get_option with update_option
  • Thinking update_option needs three parameters
5. You want to save a user preference 'font_size' only if it is a positive integer. Which code snippet correctly updates the setting safely?
hard
A.
update_option('font_size', $_POST['font_size']);
B.
$size = intval($_POST['font_size']);
if ($size > 0) {
  update_option('font_size', $size);
}
C.
if ($_POST['font_size']) {
  update_option('font_size', $_POST['font_size']);
}
D.
$size = $_POST['font_size'];
update_option('font_size', $size > 0 ? $size : 12);

Solution

  1. Step 1: Validate and sanitize the input

    $size = intval($_POST['font_size']);
    if ($size > 0) {
      update_option('font_size', $size);
    }
    uses intval to convert input to integer and checks if it is positive before saving.
  2. Step 2: Confirm safe update of option

    Only if the value is positive does it call update_option, preventing invalid data.
  3. Final Answer:

    $size = intval($_POST['font_size']);
    if ($size > 0) {
    update_option('font_size', $size);
    }
    -> Option B
  4. Quick Check:

    Validate input before update_option [OK]
Hint: Always validate input before update_option [OK]
Common Mistakes:
  • Saving raw user input without validation
  • Using update_option without checking value
  • Assuming non-empty input is always valid