Wp settings storage audit

From Lonsdale201/wp-agent-skills · View interactive version

Wp settings storage audit is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Audit how WordPress plugins and classic themes store settings and configuration. Use when reviewing get_option/update_option/add_option/register_setting/settings_fields/options.php, Customizer add_setting/get_theme_mod/set_theme_mod, theme_mod vs option decisions, associative-array option schemas, k

How to use this skill

Skill file

WP Settings Storage Audit

Use this skill to audit the persistence contract for plugin settings and classic theme settings. It complements UI-specific skills: wp-admin-settings-api explains how to render/save a classic admin form, wp-plugin-options-storage chooses the storage primitive, and classic-theme-customizer covers Customizer UI.

The core question is: can another developer safely predict where each setting is stored, what shape it has, when it is loaded, how it is sanitized, and which code path reacts after it changes?

Verdicts

| Verdict | Meaning |

|---|---|

| Correct | Each setting has the right storage primitive, stable prefixed name, documented array shape, sanitize/validate/default path, intentional autoload, and safe read/write APIs. |

| Risky | The settings work but have weak schema, autoload bloat, tab wipe risk, unclear defaults, REST schema mismatch, or Customizer/plugin boundary issues. |

| Incorrect | The code stores durable state in the wrong primitive, trusts raw request data, bypasses nonce/capability checks, misuses unregistered options through options.php, stores plugin data in theme mods, or relies on deprecated behavior. |

Audit Workflow

1. Classify the setting

Pick the primitive by ownership and access pattern:

| Data | Correct primitive |

|---|---|

| Site-wide plugin config, feature flags | One prefixed option per feature, usually an associative array |

| Per-user preference or dismissed state | User meta |

| Per-post/CPT data | Post meta registered with schema when exposed |

| Per-term data | Term meta |

| Cached recomputable value | Transient with TTL |

| Network-wide multisite config | Site option |

| Queryable, append-mostly, sortable records | Custom table |

| Theme presentation choice | theme_mod through Customizer |

Do not store plugin-owned business data, credentials, payment config, content models, or cross-theme app state in theme mods. Do not store per-user maps, logs, counters, or large datasets inside one global option.

2. Require a stable key and shape

Plugin option names must be prefixed and domain-scoped:

const OPTION = 'myplugin_settings';

For coherent plugin settings read and saved together, prefer one

associative-array option per feature:

$defaults = array(
    'enabled'       => false,
    'api_endpoint'  => '',
    'retry_minutes' => 15,
    'mode'          => 'safe',
);

$options = wp_parse_args( get_option( self::OPTION, array() ), $defaults );

Audit the array as a contract:

Separate scalar options are valid when fields have independent write cadence,

autoload policy, access control, secret handling, migration lifecycle, or atomic

update needs. Flag option-per-field storage only when it creates measurable

autoload/query sprawl or lacks a deliberate contract; do not call three small,

independent options incorrect by count alone.

Avoid anonymous JSON strings in options. WordPress already serializes arrays. If inner fields must be queried, sorted, paginated, aggregated, or partially updated, use a custom table.

3. Verify the Settings API or an equivalent custom handler

Prefer the Settings API for conventional classic plugin settings because it

centralizes registration, schema/sanitization, nonce fields, allowed options,

and REST integration:

register_setting( 'myplugin', 'myplugin_settings', array(
    'type'              => 'object',
    'default'           => $defaults,
    'sanitize_callback' => 'myplugin_sanitize_settings',
    'show_in_rest'      => false,
) );

The Settings API form posts to core:

<form method="post" action="options.php">
    <?php
    settings_fields( 'myplugin' );
    do_settings_sections( 'myplugin' );
    submit_button();
    ?>
</form>

For the Settings API path, audit these invariants:

A custom admin handler is also valid. Do not report it as insecure or incorrect

solely because it calls update_option() directly. Require equivalent controls:

register_setting() is required for options.php and /wp/v2/settings

integration, not as a universal precondition for every safe custom form.

4. Sanitize as a schema

Sanitize callbacks should return the full clean option shape:

function myplugin_sanitize_settings( $input ): array {
    $defaults = myplugin_default_settings();
    $existing = wp_parse_args( get_option( 'myplugin_settings', array() ), $defaults );
    $input    = is_array( $input ) ? $input : array();
    $clean    = $existing;

    $clean['enabled'] = ! empty( $input['enabled'] );

    if ( array_key_exists( 'retry_minutes', $input ) ) {
        $clean['retry_minutes'] = max( 1, min( 1440, absint( $input['retry_minutes'] ) ) );
    }

    if ( isset( $input['mode'] ) && in_array( $input['mode'], array( 'safe', 'fast' ), true ) ) {
        $clean['mode'] = $input['mode'];
    }

    return $clean;
}

Rules:

Core calls sanitize_option( $option, $value ), the registered sanitize_option_{$option} filters, then pre_update_option_{$option}, pre_update_option, update_option, update_option_{$option}, and updated_option.

5. Decide autoload deliberately

WP 7.1 retains the WP 6.6+ autoload model: add_option() and update_option() accept true, false, or null; internal stored values include on, off, auto, auto-on, and auto-off. The legacy strings 'yes' and 'no' are deprecated since 6.7.

Audit decisions:

Customizer type => 'option' settings can pass an autoload argument. Core defaults multidimensional Customizer option writes to autoload true unless overridden, so audit this explicitly for option-backed Customizer settings.

6. Expose REST settings only with schema

When settings are exposed through /wp/v2/settings, require show_in_rest schema that matches the stored value:

'show_in_rest' => array(
    'schema' => array(
        'type'                 => 'object',
        'additionalProperties' => false,
        'properties'           => array(
            'enabled' => array(
                'type'    => 'boolean',
                'default' => false,
            ),
            'mode' => array(
                'type' => 'string',
                'enum' => array( 'safe', 'fast' ),
            ),
        ),
    ),
),

Important core behavior:

If lower-privileged users need to write only part of a setting, create a custom REST route with its own permission and validation instead of exposing the whole option through /wp/v2/settings.

7. Audit Customizer storage

For classic themes, theme_mod is the default:

$wp_customize->add_setting( 'mytheme_layout', array(
    'type'              => 'theme_mod',
    'default'           => 'full',
    'sanitize_callback' => 'mytheme_sanitize_layout',
) );

Audit:

Core stores theme mods in theme_mods_{stylesheet}. A theme switch changes which theme-mod option is read, so plugin-like data disappears by design.

8. Check deprecations and forbidden paths

Flag these:

allowlist, schema validation, or safe write contract described above.

Report Format

Report findings in this shape:

  1. Storage map: option/theme_mod/meta/transient/custom-table names, owners, and shapes.
  2. Findings: severity, file/line, bad behavior, and the WordPress rule it violates.
  3. Fix plan: storage primitive, option name, array schema, sanitize/default merge, autoload, REST exposure, and update hooks.
  4. Migration notes: old key to new key, default backfill, autoload correction, multisite scope, and uninstall cleanup.
  5. Tests: admin save, tab save, invalid input, REST save if exposed, Customizer preview/save, frontend read, and autoload query.

Cross-References

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.