0
0
Wordpressframework~20 mins

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

Choose your learning style9 modes available
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.