Interactive Documentation Arrives in WordPress 7.1
For years, the official WordPress Code Reference served as a static repository of function definitions, class structures, hooks, and code samples. Developers browsing developer.wordpress.org/reference had to copy code snippets, switch to a local development environment, paste the snippet into a plugin or site, and run it to verify output. WordPress 7.1 fundamentally alters this workflow by introducing the first runnable code examples embedded directly into the documentation pages.
This feature allows developers to execute PHP code right inside their web browser without leaving the reference documentation. Instead of relying on static code snippets that might become outdated or require local scaffolding to run, users can now select a Run button attached to a code block to view immediate execution results. The implementation bridges the gap between static reference material and hands-on testing.
Powered by WordPress Playground: Client-Side PHP Execution
The execution engine behind the Code Reference interactive snippets is WordPress Playground. WordPress Playground leverages WebAssembly (Wasm) to run PHP and SQLite entirely within the browser tab. This architectural choice solves two historical roadblocks associated with executing code on developer documentation sites:
- Security and Isolation: Running arbitrary PHP snippets on a centralized server creates immense security risks, including potential remote code execution (RCE) vectors, server resource exhaustion, and complex sandboxing requirements. WebAssembly isolates execution entirely within the client’s web browser sandbox, eliminating server-side vulnerability vectors.
- Infrastructure Scalability: Traditional code-runner services require dedicated backend execution nodes that scale with user traffic. Because WordPress Playground offloads all PHP parsing and script execution to the client device, the documentation hub serves standard static pages while client-side Wasm handles the computation.
DocBlock Markdown Syntax: Understanding php interactive
Runnable code snippets are not managed through a separate database or external cms interface. Instead, they are defined inline within the WordPress codebase DocBlocks using standard PHPDoc annotations extended with Markdown code fence modifiers. To convert a static PHP snippet into an interactive client-side execution block, core developers append the interactive modifier to the standard php language identifier on the code fence.
Below is the exact implementation structure used in the core codebase to define an interactive snippet for a class method:
<?php
/**
* Generator for a foreach loop to step through each class name for the matched tag.
*
* ```php interactive
* $p = new WP_HTML_Tag_Processor( "<div class='free <egg>tlang-en'>" );
* $p->next_tag();
* foreach ( $p->class_list() as $class_name ) {
* echo "{$class_name} ";
* }
* // Outputs: "free <egg> lang-en "
* ```
*/
public function class_list() {}
When the core documentation parser processes source files, it identifies the ```php interactive directive. Rather than generating a plain syntax-highlighted HTML <pre> block, the parser outputs an interactive component configured to hand off the snippet payload to WordPress Playground upon activation.
Practical Example: Testing WP_HTML_Processor::class_list()
The initial deployment in WordPress 7.1 includes live runnable examples for the HTML API, specifically demonstrated on the reference page for WP_HTML_Processor::class_list() (and its underlying WP_HTML_Tag_Processor partner methods).
Consider what happens when executing the sample code inline:
$p = new WP_HTML_Tag_Processor( "<div class='free <egg>tlang-en'>" );
$p->next_tag();
foreach ( $p->class_list() as $class_name ) {
echo "{$class_name} ";
}
Executing this snippet in the browser triggers the WebAssembly PHP engine, instantiates WP_HTML_Tag_Processor, parses the HTML markup string containing special characters and tab delimiters (t), iterates through the tokenized class list, and returns the raw output directly beneath the editor window:
free <egg> lang-en
This allows developers to observe how the HTML API handles encoded entities like <egg> and whitespace normalization in real time, confirming expected behavior without setting up unit tests locally.
Technical Workflow: Parsing DocBlocks into Interactive Reference Pages
The complete pipeline from core repository code to an interactive web element follows a structured processing flow:
- Source Control: Core contributors write DocBlocks directly in PHP files within the
WordPress/wordpress-developrepository, using the```php interactivecode fence. - Parsing Engine: The automated parser scans core source files during release cycles, extracting PHPDoc blocks, parameter lists, and inline Markdown documentation.
- AST & Syntax Identification: Markdown blocks with standard
```phpfences are converted into syntax-highlighted static code snippets. Fences tagged as```php interactiveare flagged for dynamic rendering. - UI Component Rendering: On the developer reference frontend, the interactive block is rendered with a Run control button.
- Client Execution: When a developer triggers execution, the component initializes an inline WordPress Playground instance via Wasm, passes the raw PHP code to the in-browser interpreter, and captures STDOUT output to present in the interactive console.
Architectural Limitations and Browser Environment Constraints
While client-side WebAssembly execution provides immediate safety and flexibility, developers and documentation authors must account for technical boundaries inherent to the WebAssembly sandbox:
- No Arbitrary External HTTP Calls: Snippets relying on standard curl requests or remote
wp_remote_get()calls to external domains will fail unless explicitly proxied or mocked, as browser security models enforce CORS restrictions on Wasm network requests. - Memory and Compilation Overhead: The initial execution of a snippet requires loading the Wasm PHP binary. While subsequent runs within the same browser session are cached, lower-powered devices may experience a brief load latency during the initial engine spin-up.
- Stateless Execution Context: Interactive code snippets execute inside an isolated context. Modifying options or global variables in one snippet does not persist state across other snippets on different reference pages unless explicit state storage is configured.
- FileSystem Constraints: File operations occur inside an in-memory virtual filesystem provided by Emscripten/Wasm. Snippets relying on absolute pathing outside the virtual WordPress installation tree will not function as expected.
Guidelines for Core Contributors: Authoring Interactive Code Examples
With documentation updates expanding for WordPress 7.2, contributors updating or creating DocBlocks across core APIs should adhere to strict authoring guidelines:
- Ensure Self-Containment: Snippets must include all variable declarations and class instantiations required for execution. Do not assume pre-existing state.
- Focus on Deterministic Output: Prefer explicit
echoor print statements that illustrate input-to-output transformations clearly for the reader. - Avoid External Dependencies: Keep example code focused strictly on Core APIs, standard PHP utilities, and built-in classes. Do not reference external third-party plugins or external APIs.
- Keep Snippets Concise: Snippets should concisely demonstrate a single method, filter, or class capability without unnecessary boilerplate.
Roadmap: What Interactive Documentation Means for WordPress 7.2
The introduction of two runnable snippets in WordPress 7.1 served as the initial production proof-of-concept for core contributors, led by key work from contributors including Jon Surrell (@jonsurrell), Dennis Snell (@dmsnell), and Weston Ruter (@westonruter).
The team is standardizing comprehensive contributor handbook guidelines detailing syntax requirements and review criteria. The goal for WordPress 7.2 and future releases is to progressively expand interactive coverage across major core subsystems, including the Option API, Format API, Block Editor support utilities, and the rest of the HTML API family.
Frequently asked questions
What powers the runnable code examples in the WordPress Code Reference?
Runnable code examples are powered by WordPress Playground, which uses WebAssembly (Wasm) to run PHP and SQLite entirely within the user's web browser tab without server-side execution.
How do core contributors mark a code snippet as runnable in a DocBlock?
Contributors append the 'interactive' modifier to the Markdown code fence in the method DocBlock using the standard syntax: “`php interactive.
Where can developers test live runnable snippets right now?
Live runnable snippets can be tested on the WP_HTML_Processor::class_list() page in the official WordPress Code Reference.
Do interactive code snippets execute PHP code on a remote server?
No. All PHP code evaluation occurs client-side inside the browser using WebAssembly, ensuring complete isolation and security for the documentation infrastructure.
Primary reference: Review the original announcement for exact release details. This article is an independent explanation and does not reproduce the source text.
