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.