Create a plugin settings page with the WordPress Settings API
The Settings API is the official way to add an options page to the WordPress admin. It is built from three functions, register_setting(), add_settings_section() and add_settings_field(), and the form is sent to the core options.php, which checks the nonce and the rights, runs your sanitize callback and saves the value to wp_options. It is more verbose than a hand-made form, but you do not have to write the security and saving part yourself, and the page looks like the other admin screens.
Problem: A plugin needs a settings page in the admin with a few fields that are saved safely.
Solution: Register one option that stores all settings as an array, describe a section and the fields, and output the form with settings_fields() and do_settings_sections().
The code
In the main plugin file:
<?php
// 1. The page under Settings
add_action( 'admin_menu', 'helloadmin_settings_menu' );
function helloadmin_settings_menu() {
add_options_page(
__( 'HelloAdmin Settings', 'helloadmin' ), // page title
__( 'HelloAdmin', 'helloadmin' ), // menu title
'manage_options', // required capability
'helloadmin-settings', // page slug
'helloadmin_settings_page' // output function
);
}
// 2. The option, the section and the fields
add_action( 'admin_init', 'helloadmin_register_settings' );
function helloadmin_register_settings() {
register_setting( 'helloadmin_options_group', 'helloadmin_options', array(
'sanitize_callback' => 'helloadmin_sanitize_options',
'default' => array( 'api_key' => '', 'posts_per_page' => 10, 'enable_analytics' => false ),
) );
add_settings_section( 'helloadmin_general', __( 'General Settings', 'helloadmin' ), function () {
echo '<p>' . esc_html__( 'Configure the plugin options below.', 'helloadmin' ) . '</p>';
}, 'helloadmin-settings' );
add_settings_field( 'helloadmin_api_key', __( 'API Key', 'helloadmin' ), 'helloadmin_api_key_field', 'helloadmin-settings', 'helloadmin_general' );
add_settings_field( 'helloadmin_posts_per_page', __( 'Posts per page', 'helloadmin' ), 'helloadmin_posts_per_page_field', 'helloadmin-settings', 'helloadmin_general' );
add_settings_field( 'helloadmin_enable_analytics', __( 'Analytics', 'helloadmin' ), 'helloadmin_enable_analytics_field', 'helloadmin-settings', 'helloadmin_general' );
}
// 3. The fields
function helloadmin_get_options() {
return wp_parse_args( get_option( 'helloadmin_options', array() ), array( 'api_key' => '', 'posts_per_page' => 10, 'enable_analytics' => false ) );
}
function helloadmin_api_key_field() {
$opts = helloadmin_get_options();
printf( '<input type="text" name="helloadmin_options[api_key]" value="%s" class="regular-text">', esc_attr( $opts['api_key'] ) );
}
function helloadmin_posts_per_page_field() {
$opts = helloadmin_get_options();
printf( '<input type="number" name="helloadmin_options[posts_per_page]" value="%d" min="1" max="100">', absint( $opts['posts_per_page'] ) );
}
function helloadmin_enable_analytics_field() {
$opts = helloadmin_get_options();
printf(
'<label><input type="checkbox" name="helloadmin_options[enable_analytics]" value="1" %s> %s</label>',
checked( $opts['enable_analytics'], true, false ),
esc_html__( 'Enable analytics', 'helloadmin' )
);
}
// 4. Sanitize everything in one callback
function helloadmin_sanitize_options( $input ) {
$input = is_array( $input ) ? $input : array();
return array(
'api_key' => sanitize_text_field( $input['api_key'] ?? '' ),
'posts_per_page' => min( 100, max( 1, absint( $input['posts_per_page'] ?? 10 ) ) ),
'enable_analytics' => ! empty( $input['enable_analytics'] ), // an unchecked box is not sent at all
);
}
// 5. The page
function helloadmin_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( 'helloadmin_options_group' ); // hidden fields and the nonce
do_settings_sections( 'helloadmin-settings' ); // the sections and fields
submit_button();
?>
</form>
</div>
<?php
}
All settings live in one option, helloadmin_options, as an array: one database row and one get_option() call instead of one per field. The sanitize callback receives the whole array from the form and returns the clean version; the rules are the same as in sanitizing user input.
NOTE: The form must be sent to options.php, and you do not need your own nonce: settings_fields() adds it. The exception is the network admin of a multisite: options.php does not save network options, so there the form goes to edit.php?action=… with your own network_admin_edit_{action} handler.