WordPress AI Safety Layer: Add Confirmation Prompts, Dry Runs & Undo to AI Abilities
The WordPress Abilities API gives plugins, themes, and external AI clients—such as MCP tools and browser extensions—a structured registry to discover and execute site actions. A model can draft posts, update metadata, clean up stale records, or adjust WooCommerce settings in seconds. That speed is also where operational risk lives: a single misread prompt, an overly broad filter, or a hallucinated ID can trash live posts or overwrite production data.
While WordPress validates inputs against JSON Schema and checks permissions before running an ability, behavioral annotations like destructive or readonly are advisory hints for external tools—they do not block execution on their own. To protect live sites, wrap every mutating ability in a three-part safety layer: dry runs, confirmation prompts, and undo snapshots.
Core Principle: Never let an AI agent execute a one-shot destructive write. Require a preview first, bind approval to the exact input payload, and record a pre-change snapshot before modifying data.
| Safety Layer | Parameter / Mechanism | What It Prevents |
|---|---|---|
| 1. Dry Run | dry_run: true (default on writes) | Blind mutations without inspecting matched IDs or field diffs |
| 2. Confirmation | Single-use confirm_token bound to user + input hash | Unapproved destructive actions or parameter tampering after preview |
| 3. Undo Rollback | Pre-change snapshot + undo_id + post-write state hash | Irreversible mistakes and silent overwrites of newer edits |
1. Dry Runs: Preview Before Anything Changes
A dry run computes and returns what would change without touching the database, filesystem, or external APIs. Include an optional boolean dry_run property in your ability’s input_schema, and default it to true for high-impact operations so agents must explicitly opt into live execution.
- Exact targets: Return the matched post IDs, slugs, titles, or option keys rather than a generic summary.
- Field-by-field diff: Show current values alongside proposed values so the user or orchestrator can verify every change.
- Side effects: Note downstream triggers such as cache purges, email notifications, or status transitions.
2. Confirmation Prompts: Input-Bound Approval Tokens
Any ability marked with destructive => true—such as trashing published content, deleting records, changing user roles, or modifying payment settings—should block live execution until the user confirms. Avoid accepting a plain confirmed: true flag that an AI client might pass automatically on its first call.
Instead, return a short-lived, single-use confirm_token during the dry run and bind that token on the server to three values: get_current_user_id(), the ability name (for example, devnahian/trash-post-safely), and a hash of the exact sanitized input payload. If the agent alters a post ID or filter after previewing, token validation fails and requires a fresh preview.
{
"status": "dry_run",
"dry_run": true,
"ability": "devnahian/trash-post-safely",
"destructive": true,
"preview": {
"action": "wp_trash_post",
"post_id": 418,
"current_status": "publish",
"title": "Getting Started with Custom Gutenberg Blocks"
},
"confirm_token": "9f3c8a1d7e2b4065a8c1f4d9",
"message": "Dry run complete. Pass confirm_token with dry_run=false to execute."
}
3. Undo: Snapshot and Rollback When Something Goes Wrong
Even careful previews cannot catch every edge case. A change can look accurate in JSON and still break a page layout or remove required metadata. Before running the underlying write operation, capture a lightweight pre-change snapshot and return an undo_id in the execution response.
- Posts & custom post types: Snapshot core fields (
post_status,post_title,post_content,post_excerpt), modified post meta, and assigned taxonomy terms. - Site options & settings: Store the previous option value before calling
update_option(). - Conflict detection: Save a hash of the post-write state so your undo handler can detect if a human edited the record afterward and require an explicit
force: trueoverride before overwriting newer work.
Execution Pipeline & Production Guardrails
- Register cleanly on
wp_abilities_api_init: Declare baseline capabilities viarequired_capabilities, enforce object-level checks inpermission_callback, and define strictinput_schemaandoutput_schemadefinitions. - Short-circuit on
dry_run: Return the structured diff and issue a time-limitedconfirm_tokenwithout calling mutating WordPress functions. - Verify and consume the token: On live execution, verify user ID, ability name, and input hash, then delete the transient immediately so the token cannot be replayed.
- Snapshot, execute, and journal: Capture the before-state, run the WordPress write, store the undo entry with its state hash, and return the
undo_id.
Quick FAQ
Does setting destructive => true in ability annotations block execution automatically?
No. Ability annotations describe expected behavior for external tools and UI prompts, while server-side enforcement comes from your required_capabilities, permission_callback, and safety wrapper.
How long should a confirmation token stay valid?
Keep confirmation tokens short-lived—typically 10 to 15 minutes—and delete them as soon as they are consumed so old approvals cannot be reused later.
What if a human edits a post after an AI ability modifies it?
Comparing the current object hash against the post-write hash stored in your undo journal lets the undo ability warn the user instead of silently overwriting manual changes.