0
0
Wordpressframework~5 mins

Settings API in Wordpress

Choose your learning style9 modes available
Introduction

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.

You want to add a new settings page for your plugin or theme.
You need to save user preferences in the WordPress admin area.
You want to create forms that store options in the WordPress database.
You want WordPress to handle security and validation for your settings.
You want a consistent look and feel for your settings pages.
Syntax
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.

Examples
This registers a setting called 'my_plugin_option_name' under the group 'my_plugin_options_group'.
Wordpress
register_setting('my_plugin_options_group', 'my_plugin_option_name');
This adds a section titled 'My Section Title' to the 'my_plugin_page' settings page.
Wordpress
add_settings_section('my_section', 'My Section Title', 'my_section_callback', 'my_plugin_page');
This adds a field labeled 'My Field Label' inside 'my_section' on 'my_plugin_page'.
Wordpress
add_settings_field('my_field', 'My Field Label', 'my_field_callback', 'my_plugin_page', 'my_section');
Sample Program

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.

Wordpress
<?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
}
?>
OutputSuccess
Important Notes

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.

Summary

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.