settings_fields

Home » Functions » settings_fields

The "settings_fields" function is a built-in WordPress function used to display the settings fields for a specific settings page. This function prints out hidden form fields that are used to authenticate options pages and ensure that the data being submitted is valid.

The "settings_fields" function is typically used in the callback function that is registered to handle the display of the settings page. By including this function in the output of the page, WordPress is able to properly handle the submission of settings data when the page is saved.

Here’s an example of the usage code for the "settings_fields" function:

function myplugin_settings_page() {
    // Output the settings page HTML
    ?>
    <div class="wrap">
        <h1>My Plugin Settings</h1>
        <form method="post" action="options.php">
            <?php
            // Output the settings fields
            settings_fields( 'myplugin_options' );

            // Output the settings sections and fields
            do_settings_sections( 'myplugin_options' );
            ?>
            <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Settings' ); ?>" />
        </form>
    </div>
    <?php
}

In this example, the "myplugin_settings_page" function is the callback function that is registered to handle the display of the settings page. Inside this function, we output the HTML for the settings page, including the form element with its action attribute set to "options.php". We then call the "settings_fields" function, passing in the name of the options group that the settings fields belong to. Finally, we call the "do_settings_sections" function to output the settings sections and fields for the given options group.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.