0
0
Wordpressframework~20 mins

Settings API in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Settings API 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 API code?

Consider this code snippet that registers a setting and adds a settings field. What will be displayed on the settings page for the field?

Wordpress
<?php
function myplugin_settings_init() {
  register_setting('myplugin_options_group', 'myplugin_option_name');
  add_settings_section('myplugin_section', 'My Plugin Settings', null, 'myplugin');
  add_settings_field('myplugin_field', 'My Option', function() {
    $value = get_option('myplugin_option_name', 'default');
    echo '<input type="text" name="myplugin_option_name" value="' . esc_attr($value) . '" />';
  }, 'myplugin', 'myplugin_section');
}
add_action('admin_init', 'myplugin_settings_init');
AA text input box with the saved option value or 'default' if none saved
BA checkbox input that is always unchecked
CA dropdown select with no options
DAn error message saying 'Invalid setting field'
Attempts:
2 left
💡 Hint

Look at how the input field is generated and what value it uses.

state_output
intermediate
1:30remaining
What is the value of the option after this code runs?

Given this code that updates an option using the Settings API, what will be the value stored in the database for 'myplugin_option_name'?

Wordpress
<?php
update_option('myplugin_option_name', 'new_value');
$value = get_option('myplugin_option_name');
Anull
B'new_value'
Cfalse
D'default'
Attempts:
2 left
💡 Hint

What does update_option do to the stored value?

📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in this Settings API code?

Which of these code snippets will cause a syntax error when registering a setting in WordPress?

Aregister_setting('myplugin_options_group', 'myplugin_option_name', ['sanitize_callback' => 'sanitize_text_field']);
Bregister_setting('myplugin_options_group', 'myplugin_option_name');
Cregister_setting('myplugin_options_group', 'myplugin_option_name', array('type' => 'string'));
Dregister_setting('myplugin_options_group' 'myplugin_option_name');
Attempts:
2 left
💡 Hint

Check the syntax of function arguments and commas.

🔧 Debug
advanced
2:00remaining
Why does this Settings API field not save the input value?

This code adds a settings field but the input value is not saved after submitting the form. What is the likely cause?

Wordpress
<?php
function myplugin_settings_init() {
  register_setting('myplugin_options_group', 'myplugin_option_name');
  add_settings_section('myplugin_section', 'My Plugin Settings', null, 'myplugin');
  add_settings_field('myplugin_field', 'My Option', function() {
    $value = get_option('myplugin_option_name', '');
    echo '<input type="text" name="myplugin_option_name" value="' . esc_attr($value) . '" />';
  }, 'myplugin', 'myplugin_section');
}
add_action('admin_init', 'myplugin_settings_init');
AThe input field name does not match the registered option name
BThe register_setting function is missing the sanitize callback
CThe settings section callback is missing
DThe add_settings_section function is missing a title
Attempts:
2 left
💡 Hint

Check the name attribute of the input field and the option name registered.

🧠 Conceptual
expert
1:30remaining
Which statement best describes the role of the Settings API in WordPress?

Choose the statement that correctly explains what the WordPress Settings API does.

AIt handles user authentication and permission checks for admin pages
BIt automatically creates database tables for plugin data storage
CIt provides a standardized way to create, display, and save settings fields and sections in the WordPress admin area
DIt is a theme framework for styling WordPress admin pages
Attempts:
2 left
💡 Hint

Think about what the Settings API helps developers do with plugin options.