Lecture 10: Runtime Safety Guards
The Role of the Must-Use Plugin (wp-seo-guard.php)
Static analysis (linting and the tokenizer-based safety scanner) catches the most common mistakes before commit. But what happens if an agent writes syntactically valid code that produces a dynamic runtime fault?
Example: A recursive rewrite rule that causes an infinite 301 redirect loop, or a dynamic query that evaluates to noindex only on the live homepage.
The Runtime Shield
By deploying tools/wp-seo-guard.php to wp-content/mu-plugins/, we establish an active defense layer that runs on every HTTP request:
- Homepage NoIndex Neutralizer: Intercepts
wp_robotsat priority9999. Ifis_front_page()oris_home()containsnoindex, it strips the directive and logs an emergency alert todebug.log. A site with "Discourage search engines" enabled (staging) keeps its noindex. - Canonical URL Normalizer: Filters
get_canonical_url(applied bywp_get_canonical_url()): forceshttps://on HTTPS sites and aligns the trailing slash with the permalink structure, so the canonical never points to a redirect. - Self-Redirect Trap: Filters
wp_redirect. When a redirect targets the URL being requested (including the classicis_ssl()loop behind a TLS proxy), the guard halts with HTTP 503 and a human-readable diagnostic message. Multi-hop loops (A → B → A) span several requests: detect them with an external crawl such ascurl -IL --max-redirs 5.
These guards contain the most common runtime faults. They complement, not replace, the static verification gates and a post-deploy crawl.