What does this code do when the settings page loads?
medium
A. Displays a text input with the saved value of 'my_option_name' inside 'My Section'
B. Shows an error because the callback function is missing
C. Creates a checkbox input instead of a text input
D. Does not display any input because settings_fields is missing
Solution
Step 1: Analyze the field callback
The callback outputs a text input with the name 'my_option_name' and sets its value to the saved option using get_option('my_option_name').
Step 2: Check form functions
settings_fields('my_option_group') outputs necessary nonce and hidden fields, and do_settings_sections('my_page') outputs the registered sections and fields.
Final Answer:
Displays a text input with the saved value of 'my_option_name' inside 'My Section' -> Option A
Quick Check:
Field callback outputs input with saved value [OK]
Hint: Field callback outputs input; settings_fields and do_settings_sections show it [OK]
Common Mistakes:
Assuming callback is missing or broken
Thinking input type is checkbox
Forgetting settings_fields is present
4. Identify the error in this WordPress Settings API code snippet:
register_setting('my_group', 'my_option');
add_settings_section('section_id', 'Section Title', null, 'my_page');
add_settings_field('field_id', 'Field Title', 'my_field_callback', 'my_page', 'section_id');
function my_field_callback() {
echo '';
}
// In the form:
settings_fields('wrong_group');
do_settings_sections('my_page');
What is the main problem here?
medium
A. The add_settings_section callback is required but set to null
B. The callback function is missing
C. The add_settings_field is missing the page parameter
D. The settings_fields function uses a different group name than register_setting
Solution
Step 1: Check group names consistency
register_setting uses 'my_group' but settings_fields uses 'wrong_group'. They must match for nonce and saving to work.
Step 2: Verify other parts
The callback function exists, page parameter is present, and section callback can be null (optional).
Final Answer:
The settings_fields function uses a different group name than register_setting -> Option D
Quick Check:
Group names must match in register_setting and settings_fields [OK]
Hint: Match group names in register_setting and settings_fields exactly [OK]
Common Mistakes:
Ignoring mismatch of group names
Thinking section callback cannot be null
Assuming missing page parameter
5. You want to create a settings page with two sections: 'General' and 'Advanced'. Each section has multiple fields. Which sequence of Settings API functions correctly sets up this page?