--- name: settings-options-security description: > Use when building an options or settings page with the WordPress Settings API - register_setting, add_settings_field, settings_fields, an options.php form, or update_option / get_option on plugin data. Attaches a sanitize_callback to every setting, gates the page with manage_options, relies on Settings API nonce handling, and escapes options on output. Prevents stored XSS and unauthorized option writes. compatibility: "Examples generally use PHP 7.4 syntax; check each API against target WordPress/PHP versions. Use maintained WordPress and supported PHP in production. Shell examples require their named tools." license: MIT metadata: tags: "wordpress, security, php, settings, options, admin, xss" --- # Settings & options security ## When to use this skill Use this skill whenever a plugin or theme stores configuration in WordPress options: - Building a settings page with the Settings API (`register_setting`, `add_settings_section`, `add_settings_field`). - A form that posts to `options.php`. - Calling `update_option()` or `add_option()` from a custom handler. - Reading options with `get_option()` and rendering them anywhere (admin, front-end, emails). The Settings API gives you nonce handling, capability gating, and a structured sanitization hook for free — but only if you actually wire it. Bypassing it for a "quick" custom form is a common source of stored XSS and unauthorized writes. Related: see the `input-sanitization-validation` skill for per-request sanitization patterns and the `nonces-csrf-protection` skill for custom-form nonce handling. ## Core principles (and why they matter) 1. **Every registered setting needs a `sanitize_callback`.** This is the single most important line of defense against stored XSS and malformed data. Without it, `update_option()` saves raw `$_POST` values verbatim. 2. **The Settings API supplies the nonce only when you use `settings_fields()`.** If you build a custom form that calls `options.php`, include `settings_fields( $option_group )` or you lose CSRF protection. 3. **Options are untrusted on read.** Even if you sanitized on save, escape again on output. A compromised database, a bad import, or a future bug can reintroduce dangerous values. 4. **Gate the page with a capability, not just a menu position.** `add_options_page()` already requires `manage_options` by default; custom menus should too. 5. **Use typed defaults.** Pass a `default` value to `register_setting()` so `get_option()` returns a known shape instead of `false`. 6. **If you expose settings via REST (`show_in_rest`), provide a schema.** Untyped REST options are another stored-XSS vector. ## Step-by-step implementation 1. On `admin_init`, call `register_setting()` with: - `option_group` matching the page. - `option_name` for the stored option. - `sanitize_callback` pointing to a strict sanitizer. - `default` with a safe value. 2. Add sections and fields with `add_settings_section()` and `add_settings_field()`. 3. Render the form posting to `options.php` and call `settings_fields( $option_group )`. 4. In field callbacks, escape the current option value with `esc_attr()` / `esc_textarea()`. 5. For custom (non-`options.php`) handlers, add your own `wp_nonce_field()` and verify it with `check_admin_referer()`, plus `current_user_can( 'manage_options' )`. 6. When displaying saved options anywhere, escape for the output context. ### Supporting references | Reference | Load when | | --- | --- | | [Settings & options security checklist](references/checklist.md) | Before final verification of the settings & options security controls. | | [Secure Settings API page](references/secure-settings-page.php) | Implementing a sanitized Settings API page with an options.php form and escaped output. | ## Common AI mistakes / anti-patterns ### Mistake 1 — `register_setting()` with no sanitize callback ```php // ❌ Insecure: raw POST data is saved into the option. register_setting( 'my_plugin_group', 'my_plugin_options' ); ``` ```php // ✅ Secure: every setting has a sanitize_callback. register_setting( 'my_plugin_group', 'my_plugin_options', array( 'type' => 'array', 'sanitize_callback' => 'my_plugin_sanitize_options', 'default' => array( 'api_key' => '', 'enabled' => 0 ), ) ); function my_plugin_sanitize_options( $input ) { $clean = array(); if ( isset( $input['api_key'] ) ) { $clean['api_key'] = sanitize_text_field( wp_unslash( $input['api_key'] ) ); } $clean['enabled'] = ! empty( $input['enabled'] ) ? 1 : 0; return $clean; } ``` ### Mistake 2 — Custom settings form bypassing `settings_fields` ```php // ❌ Insecure: no nonce, no capability gate, raw option write.
``` ### Mistake 3 — Echoing `get_option()` unescaped ```php // ❌ Insecure: stored XSS if the option contains JavaScript. echo '