The Settings API helps you easily create and manage settings pages in WordPress without writing a lot of code. It keeps your settings organized and safe.
Settings API in Wordpress
register_setting( $option_group, $option_name, $args ); add_settings_section( $id, $title, $callback, $page ); add_settings_field( $id, $title, $callback, $page, $section, $args );
register_setting links your option to a group and handles saving.
add_settings_section creates a section on the settings page to group fields.
register_setting('my_plugin_options_group', 'my_plugin_option_name');
add_settings_section('my_section', 'My Section Title', 'my_section_callback', 'my_plugin_page');
add_settings_field('my_field', 'My Field Label', 'my_field_callback', 'my_plugin_page', 'my_section');
This code creates a settings page under the WordPress Settings menu. It registers one text option, shows a section with a description, and a text input field. The form saves the option safely using the Settings API.
<?php // Hook to admin menu to add settings page add_action('admin_menu', function() { add_options_page('My Plugin Settings', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_settings_page'); }); // Hook to admin init to register settings add_action('admin_init', function() { register_setting('my_plugin_options_group', 'my_plugin_option'); add_settings_section( 'my_plugin_section', 'My Plugin Settings Section', function() { echo '<p>Adjust your settings below:</p>'; }, 'my-plugin' ); add_settings_field( 'my_plugin_field', 'Enter Text', function() { $value = get_option('my_plugin_option', ''); echo "<input type='text' name='my_plugin_option' value='" . esc_attr($value) . "' />"; }, 'my-plugin', 'my_plugin_section' ); }); // Settings page HTML function my_plugin_settings_page() { ?> <div class="wrap"> <h1>My Plugin Settings</h1> <form method="post" action="options.php"> <?php settings_fields('my_plugin_options_group'); do_settings_sections('my-plugin'); submit_button(); ?> </form> </div> <?php } ?>
Always use esc_attr() when outputting saved values in input fields to keep data safe.
Use settings_fields() and do_settings_sections() inside your form to connect your settings properly.
Settings API handles security checks like nonce verification automatically when you use it correctly.
The Settings API helps you build settings pages easily and safely.
Use register_setting, add_settings_section, and add_settings_field to organize your options.
Always wrap your settings form with settings_fields and do_settings_sections for proper saving and display.
Practice
Solution
Step 1: Understand the role of Settings API
The Settings API is designed to help developers create settings pages and manage options in a standardized and secure way.Step 2: Compare with other WordPress features
User authentication, database optimization, and custom post types are handled by other parts of WordPress, not the Settings API.Final Answer:
To create and manage settings pages and options safely and easily -> Option CQuick Check:
Settings API = Manage settings pages [OK]
- Confusing Settings API with user login functions
- Thinking it manages posts or taxonomies
- Assuming it optimizes database queries
Solution
Step 1: Identify function roles
register_setting()registers a setting group and option name for saving options.Step 2: Differentiate from other functions
add_settings_section()adds a section,add_settings_field()adds a field, andsettings_fields()outputs nonce fields for the form.Final Answer:
register_setting() -> Option AQuick Check:
Register setting = register_setting() [OK]
- Using add_settings_section to register options
- Confusing settings_fields with registration
- Mixing add_settings_field with option registration
register_setting('my_option_group', 'my_option_name');
add_settings_section('my_section', 'My Section', null, 'my_page');
add_settings_field('my_field', 'My Field', function() { echo ''; }, 'my_page', 'my_section');
// In the form:
settings_fields('my_option_group');
do_settings_sections('my_page');What does this code do when the settings page loads?
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, anddo_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 AQuick Check:
Field callback outputs input with saved value [OK]
- Assuming callback is missing or broken
- Thinking input type is checkbox
- Forgetting settings_fields is present
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?
Solution
Step 1: Check group names consistency
register_settinguses 'my_group' butsettings_fieldsuses '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 DQuick Check:
Group names must match in register_setting and settings_fields [OK]
- Ignoring mismatch of group names
- Thinking section callback cannot be null
- Assuming missing page parameter
Solution
Step 1: Register all settings first
Useregister_settingfor each option before adding sections and fields.Step 2: Add sections before fields
Sections must be added before fields so fields can attach to existing sections.Step 3: Add fields to correct sections
Useadd_settings_fieldwith correct section IDs and page slug.Final Answer:
Registers settings first, adds sections then fields in the correct order -> Option BQuick Check:
Register settings -> add sections -> add fields [OK]
- Adding fields before sections exist
- Registering settings after adding fields
- Mixing order of sections and fields
Ready to prove it?
Pass the test and this pattern moves unknown
