跳到正文
DEV Community· Osaka_WebDeveloper·· 2 小時前AI 評分51

Claude Code 如何避免破壞 WordPress 正式網站:CLAUDE.md 規則與 Hooks 防護

Keeping Claude Code from Breaking Live WordPress Sites: Rules in CLAUDE.md, Teeth in Hooks

AI 導讀

作者分享如何以 CLAUDE.md 操作規則、Hooks 和權限 deny 清單,為 Claude Code 維護中的 WordPress 正式網站設置分層防護。

正文

I do WordPress maintenance work for clients — the unglamorous kind: plugin updates on live sites, layout breakage after someone else's update, server migrations that have to happen without taking the contact form (or the mail server) down with them.

For the past few months I've been doing a lot of this work with Claude Code open next to me. It's genuinely good at the investigation side of maintenance: reading error logs, tracing which plugin update coincided with a layout change, inventorying a site before a migration. But maintenance has a property that greenfield development doesn't — the site is already live, already making the client money, and the worst outcome isn't "the code is ugly," it's "the site is down and the client found out before I did."

So before I let an AI agent touch that kind of work, I wanted two layers of protection:

  1. Written rules the agent reads at the start of every session (CLAUDE.md).
  2. Mechanical blocks that stop the truly dangerous operations no matter what the agent — or I — was thinking (hooks and permission rules).

This post is about how I set up both. Everything below is the actual configuration I work with.

Layer 1: CLAUDE.md is where you write the operating rules

Claude Code reads a CLAUDE.md file in your project folder at the start of a session. For a maintenance project, mine is built around one idea: the rules for an existing live site are mostly about not breaking things. The sections that earn their keep:

Non-negotiables. The rules I never want negotiated away mid-session:

  • Never update, edit, or delete anything on production without approval. Present the scope of impact and how to roll back first.
  • Update the theme and plugins one at a time, and check the key screens right after each one.
  • Never edit the parent theme or WordPress core directly. Route changes through the child theme or a small dedicated plugin.
  • Before changing wp-config.php, .htaccess, or database contents, save a copy of the current state.
  • Never add or remove admin users, or change where email is sent, without the client's confirmation.

An incident triage order. When something is broken, an agent left to improvise will often start changing things. I give it a fixed order instead: read the error logs first (PHP errors, the host's log, wp-content/debug.log), line the timing up against recent changes, isolate suspect plugins starting with the lowest-impact ones, determine whether the fault is in the theme or on the server side — and only then propose a fix. Report the cause and the evidence before fixing.

Reporting format. Every session ends with a work log: what changed, why, what was deliberately not changed (updates I decided to hold, and why), and watch-outs carried over to next time. That last part — the deliberate non-changes — turned out to matter most for client trust.

One honest caveat: CLAUDE.md works by instruction, not enforcement. It's a strong nudge, not a guarantee. Which is why layer 2 exists.

Layer 2: Hooks stop what prose can't

Claude Code supports hooks — shell commands that run at defined points. A PreToolUse hook fires before a tool executes, and if the hook script exits with code 2, the operation is blocked and the script's stderr is shown to Claude as feedback.

Here is the hook I use to keep data-destroying commands from ever running inside a session. It reads the JSON Claude Code passes on stdin, pulls out the Bash command that's about to run, and blocks the patterns I never want executed unattended:

#!/usr/bin/env bash
# PreToolUse hook: stop irreversible Bash commands before they run.
# Requires: jq

set -euo pipefail

input=$(cat)
command=$(printf '%s' "$input" | jq -r '.tool_input.command // empty')

case "$command" in
  *"rm -rf"* | *"wp db reset"* | *"wp site empty"* | *"DROP TABLE"* | *"DROP DATABASE"* | *"wp db drop"*)
    echo "Blocked by hook (data-destroying operation): ${command}" >&2
    echo "If this is really needed, review the target and the blast radius first, then run it manually." >&2
    exit 2
    ;;
esac

exit 0

And the matching .claude/settings.json that wires it up, plus a permission deny list as a second net:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-dangerous-bash.sh" }
        ]
      }
    ]
  },
  "permissions": {
    "deny": [
      "Edit(./wp-config.php)",
      "Write(./wp-config.php)",
      "Bash(rm -rf *)",
      "Bash(wp db reset *)",
      "Bash(wp site empty *)"
    ]
  }
}

(Don't forget chmod +x on the hook script, or it silently won't run.)

Two details worth copying if you adapt this:

  • Narrow the pattern, don't loosen it. If a blocked command turns out to be something the project legitimately needs, the fix is a more specific pattern — not removing the rule. The deny list is short on purpose; a list of fifty rules would start blocking everyday work and I'd end up disabling it.
  • Block edits to wp-config.php, not reads. During an investigation, Claude legitimately needs to read the config. Denying reads would just make troubleshooting worse. The deny rules above only cover Edit/Write.

A second hook runs php -l via a PostToolUse hook whenever a PHP file is edited, so a syntax error surfaces in the session instead of on the staging server. Same pattern, smaller payoff, still worth it.

The same thinking, applied to procedures

The other thing I ended up formalizing is procedures — the checks that have to happen in the same order every time. In Claude Code these live as skills (a folder under .claude/skills/ with a SKILL.md describing the procedure, invoked like /wp-migration-precheck).

My most-used one is the pre-migration check. Migration incidents almost always come from "we had no idea that setting existed" surfacing on moving day, so the skill walks a fixed inventory: versions and sizes, theme/plugin list and which plugins are server-dependent, DNS and SSL — and, the one that bites people, where the MX records point. If mail runs on the same domain and you move the site carelessly, you can take the client's email down along with it. Anything that can't be confirmed gets marked "unconfirmed" rather than guessed, because a quote built on guesses is how migrations lose money.

I have similar skills for layout-breakage triage, a read-only security audit, and backup verification (checking the backup is a real, restorable file — not just that a plugin says "enabled").

If you want the whole setup

I've packaged the full configuration — the CLAUDE.md templates (maintenance, theme dev, plugin dev, plus one for Google Apps Script projects), five skills, checklists, and the settings/hooks above — as a kit called the Claude Code × WordPress / GAS Field Kit. It's on Gumroad for $29, and the hook script and maintenance CLAUDE.md template are available as a free sample if you want to look before deciding anything:

Free sample / full kit on Gumroad

Full disclosure: I made the kit and I sell it. Everything in this article — the hook, the settings, the rules — is complete on its own; the kit is just the rest of the templates and procedures in one place.

I'd be interested in how others are handling the "agent on a production site" problem — especially what you've put in the mechanically-blocked category versus the please-don't category.

Disclosure: this article was drafted with AI assistance and reviewed/edited by me before posting, per DEV's AI-assisted article guidelines.

來源:DEV Community · dev.to