Skip to content

Lesson 5 of 9 · 26m

Settings API and Admin Screens That Belong in wp-admin

Register settings correctly, build predictable screens, respect capabilities and nonces, and make operational state visible instead of hiding it in mystery widgets.

The admin interface is where your plugin earns trust. Site operators can forgive an ugly frontend experiment; they do not forgive a settings page that silently fails to save, exposes configuration to editors, or hides critical operational state behind clever UI. Good wp-admin work is intentionally unglamorous: predictable forms, visible feedback, native styling, and exact capability checks.

Register settings before rendering fields

add_action('admin_init', function (): void {
    register_setting('acme_settings', 'acme_options', [
        'type'              => 'array',
        'sanitize_callback' => 'acme_sanitize_options',
        'default'           => ['mode' => 'test', 'warehouse_id' => 0],
    ]);

    add_settings_section('acme_main', 'Inventory Sync', '__return_null', 'acme-settings');
    add_settings_field('mode', 'Mode', 'acme_render_mode_field', 'acme-settings', 'acme_main');
});

register_setting() is not ceremony. It tells WordPress which option group may be submitted, how the value is sanitized, and whether the user has permission. If you skip it and manually process $_POST, you usually reimplement nonces, capabilities, validation, and error handling badly.

Sanitizers receive untrusted input on every save, including values omitted by malformed requests:

function acme_sanitize_options($input): array {
    $input = is_array($input) ? $input : [];
    return [
        'mode' => in_array($input['mode'] ?? '', ['test', 'live'], true) ? $input['mode'] : 'test',
        'warehouse_id' => absint($input['warehouse_id'] ?? 0),
    ];
}

Build screens with native primitives

add_action('admin_menu', function (): void {
    add_menu_page('Acme Inventory', 'Inventory', 'manage_options', 'acme-inventory', 'acme_render_screen', 'dashicons-database', 58);
});

Inside the screen, use <form method="post" action="options.php">, settings_fields('acme_settings'), do_settings_sections('acme-settings'), and submit_button(). WordPress handles the nonce and redirect flow. For custom non-settings forms, call wp_nonce_field() and verify with check_admin_referer().

Respect the capability you declared. If the menu uses manage_options, individual destructive operations may need stronger or narrower checks. Do not render a disabled-looking delete button while accepting its POST.

Operational screens need truth, not marketing

A useful plugin screen answers:

  • Is integration configured correctly?
  • When did the last sync run?
  • How many records succeeded, failed, or were skipped?
  • What error will happen next if nobody intervenes?
  • Can I retry safely without duplicating work?
printf(
    '<div class="notice notice-error"><p>%s</p></div>',
    esc_html(sprintf('Sync failed at %s: %s', $time, $error))
);

Use admin notices sparingly and scope them to your screen. A plugin that injects upsell banners across every wp-admin page is training customers to hate it.

List tables, bulk actions, and scale

WP_List_Table is old but serviceable for admin grids. The important part is not the base class; it is pagination. Never load all rows to format them in PHP. Query one page, total rows separately, and implement bulk actions with object-level authorization.

For recurring destructive actions, require confirmation and use explicit audit fields: who requested it, when, which batch, and what happened. In production, “the intern clicked it” is not a recovery strategy.

Field notes

  • Load admin assets only on your screen using the $hook_suffix returned by add_menu_page().
  • settings_errors() after save is how users know validation rejected something.
  • Keep secrets write-only where possible: show “configured” instead of echoing an API token into HTML.
  • Test with a non-administrator account. If the menu disappears but the URL still works, your capability check is in the wrong place.

Was this lesson helpful?

Sign in to rate