From c22e2943bffff4185c09b593ddd4c7ec0ce75647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 4 Sep 2026 12:30:31 +0200 Subject: [PATCH 1/8] Document runnable code examples in DocBlocks --- index.md | 1 + inline-documentation-standards/php.md | 2 + .../runnable-examples.md | 107 ++++++++++++++++++ manifest.json | 6 + 4 files changed, 116 insertions(+) create mode 100644 inline-documentation-standards/runnable-examples.md diff --git a/index.md b/index.md index d7c7e94..5613358 100644 --- a/index.md +++ b/index.md @@ -15,6 +15,7 @@ WordPress is a big project with thousands of contributors. It's important that b diff --git a/inline-documentation-standards/php.md b/inline-documentation-standards/php.md index 5096c94..ef1e3f3 100644 --- a/inline-documentation-standards/php.md +++ b/inline-documentation-standards/php.md @@ -133,6 +133,8 @@ HTML markup should never be used outside of code examples, though Markdown can b * @link https://core.trac.wordpress.org/ticket/20000 ``` +For guidance on making a PHP example runnable in the Code Reference, see [Runnable code examples in DocBlocks](https://developer.wordpress.org/coding-standards/inline-documentation-standards/runnable-examples/). + #### `@since` Section (Changelogs) Every function, hook, class, and method should have a corresponding `@since` version associated with it (more on that below). diff --git a/inline-documentation-standards/runnable-examples.md b/inline-documentation-standards/runnable-examples.md new file mode 100644 index 0000000..ea2c0fe --- /dev/null +++ b/inline-documentation-standards/runnable-examples.md @@ -0,0 +1,107 @@ +# Runnable code examples in DocBlocks + +A DocBlock example can be marked as runnable. The Code Reference then shows it with a **Run** button, and readers execute and edit it in the browser through [WordPress Playground](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). + +This is opt-in. Existing indented examples keep working. Convert an example only when running it teaches something reading it does not. + +## Example +````php +/** + * Generator for a foreach loop to step through each class name for the matched tag. + * + * ```php interactive + * $p = new WP_HTML_Tag_Processor( "
" ); + * $p->next_tag(); + * foreach ( $p->class_list() as $class_name ) { + * echo "{$class_name} "; + * } + * // Outputs: free lang-en + * ``` + */ +```` + +Three parts: a fenced block, the info string `php interactive`, and a trailing `// Outputs:` comment. + +## The fence + +- `php interactive`, lowercase, in that order. Anything else (`PHP interactive`, `php-interactive`, `php title="x" interactive`) renders as plain code. +- May be indented, including inside a list item. Code lines need not match the fence indentation. +- Rendered in place, between the surrounding paragraphs. + +## The code + +Start with the first meaningful line. Do not include `' ); +// Outputs: +``` + +Several lines — empty `// Outputs:` starts a block, each `//` line is one output line, a final empty `//` keeps a trailing newline: +```php +print_r( array( 'fruit' => 'apple' ) ); +// Outputs: +// Array +// ( +// [fruit] => apple +// ) +// +``` + +JSON-encoded — for trailing spaces, tabs, or repeated newlines. Must be one JSON string: +```php +echo "done "; +// Outputs (JSON-encoded): "done " +``` + +Only a trailing standalone line comment counts. `// Outputs:` inside a string, a block comment, after code on the same line, or followed by more code is treated as code. The older ` ```expected-output ` fence still parses; do not use both. + +## Hidden setup + +If the example needs a helper function, a post, or an option, put that in a setup Blueprint ([Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) JSON). It runs first and never appears on the page. + +Inline, directly before or after the PHP fence: +````arduino +```setup-blueprint +{ "steps": [ { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/docs-fixture.php", "data": "`. A named Blueprint in the file-level DocBlock is available to every example in the file: +````go +```setup-blueprint shared-greeting +{ "steps": [ ... ] } +``` +```php interactive setup-blueprint=shared-greeting +echo docs_shared_greeting( 'first' ); +// Outputs: Hello, first +``` +```` + +Names are case-sensitive. `blueprint=` and `setupblueprint=` are not recognized. + +## Before you commit + +- Fence reads exactly `php interactive`. +- No `` on any HTML page per the [Playground guide](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). + +## Timing and feedback + +The Code Reference is built from the current stable release, so an example in `trunk` appears when that release ships. + +Questions: `#live-doc-snippets` on Make WordPress Slack. Parser bugs: [phpdoc-parser](https://github.com/WordPress/phpdoc-parser/issues). Rendering bugs: [wporg-developer](https://github.com/WordPress/wporg-developer/issues). diff --git a/manifest.json b/manifest.json index 0fe8c6d..f6e7ae9 100644 --- a/manifest.json +++ b/manifest.json @@ -61,6 +61,12 @@ "parent": "inline-documentation-standards", "markdown_source": "https://github.com/WordPress/wpcs-docs/blob/master/inline-documentation-standards/php.md" }, + "inline-documentation-standards/runnable-examples": { + "title": "Runnable code examples in DocBlocks", + "slug": "runnable-examples", + "parent": "inline-documentation-standards", + "markdown_source": "https://github.com/WordPress/wpcs-docs/blob/master/inline-documentation-standards/runnable-examples.md" + }, "changelog": { "title": "Changelog", "slug": "changelog", From 12ad33b9625f87dd139a5ff983082487a8c2dc69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 4 Sep 2026 12:47:16 +0200 Subject: [PATCH 2/8] Use empty language for nested Markdown examples --- inline-documentation-standards/runnable-examples.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inline-documentation-standards/runnable-examples.md b/inline-documentation-standards/runnable-examples.md index ea2c0fe..2daab12 100644 --- a/inline-documentation-standards/runnable-examples.md +++ b/inline-documentation-standards/runnable-examples.md @@ -68,7 +68,7 @@ Only a trailing standalone line comment counts. `// Outputs:` inside a string, a If the example needs a helper function, a post, or an option, put that in a setup Blueprint ([Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) JSON). It runs first and never appears on the page. Inline, directly before or after the PHP fence: -````arduino +```` ```setup-blueprint { "steps": [ { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/docs-fixture.php", "data": "`. A named Blueprint in the file-level DocBlock is available to every example in the file: -````go +```` ```setup-blueprint shared-greeting { "steps": [ ... ] } ``` From f9abd745fb2fcd7b42961b8966afc700f8c43502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 4 Sep 2026 12:49:10 +0200 Subject: [PATCH 3/8] Place runnable examples in PHP documentation --- index.md | 1 - inline-documentation-standards/php.md | 108 +++++++++++++++++- .../runnable-examples.md | 107 ----------------- manifest.json | 6 - 4 files changed, 107 insertions(+), 115 deletions(-) delete mode 100644 inline-documentation-standards/runnable-examples.md diff --git a/index.md b/index.md index 5613358..d7c7e94 100644 --- a/index.md +++ b/index.md @@ -15,7 +15,6 @@ WordPress is a big project with thousands of contributors. It's important that b diff --git a/inline-documentation-standards/php.md b/inline-documentation-standards/php.md index ef1e3f3..da654db 100644 --- a/inline-documentation-standards/php.md +++ b/inline-documentation-standards/php.md @@ -133,7 +133,113 @@ HTML markup should never be used outside of code examples, though Markdown can b * @link https://core.trac.wordpress.org/ticket/20000 ``` -For guidance on making a PHP example runnable in the Code Reference, see [Runnable code examples in DocBlocks](https://developer.wordpress.org/coding-standards/inline-documentation-standards/runnable-examples/). +#### Runnable examples + +A DocBlock example can be marked as runnable. The Code Reference then shows it with a **Run** button, and readers execute and edit it in the browser through [WordPress Playground](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). + +This is opt-in. Existing indented examples keep working. Convert an example only when running it teaches something reading it does not. + +##### Example +````php +/** + * Generator for a foreach loop to step through each class name for the matched tag. + * + * ```php interactive + * $p = new WP_HTML_Tag_Processor( "
" ); + * $p->next_tag(); + * foreach ( $p->class_list() as $class_name ) { + * echo "{$class_name} "; + * } + * // Outputs: free lang-en + * ``` + */ +```` + +Three parts: a fenced block, the info string `php interactive`, and a trailing `// Outputs:` comment. + +##### The fence + +- `php interactive`, lowercase, in that order. Anything else (`PHP interactive`, `php-interactive`, `php title="x" interactive`) renders as plain code. +- May be indented, including inside a list item. Code lines need not match the fence indentation. +- Rendered in place, between the surrounding paragraphs. + +##### The code + +Start with the first meaningful line. Do not include `' ); +// Outputs: +``` + +Several lines — empty `// Outputs:` starts a block, each `//` line is one output line, a final empty `//` keeps a trailing newline: +```php +print_r( array( 'fruit' => 'apple' ) ); +// Outputs: +// Array +// ( +// [fruit] => apple +// ) +// +``` + +JSON-encoded — for trailing spaces, tabs, or repeated newlines. Must be one JSON string: +```php +echo "done "; +// Outputs (JSON-encoded): "done " +``` + +Only a trailing standalone line comment counts. `// Outputs:` inside a string, a block comment, after code on the same line, or followed by more code is treated as code. The older ` ```expected-output ` fence still parses; do not use both. + +##### Hidden setup + +If the example needs a helper function, a post, or an option, put that in a setup Blueprint ([Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) JSON). It runs first and never appears on the page. + +Inline, directly before or after the PHP fence: +```` +```setup-blueprint +{ "steps": [ { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/docs-fixture.php", "data": "`. A named Blueprint in the file-level DocBlock is available to every example in the file: +```` +```setup-blueprint shared-greeting +{ "steps": [ ... ] } +``` +```php interactive setup-blueprint=shared-greeting +echo docs_shared_greeting( 'first' ); +// Outputs: Hello, first +``` +```` + +Names are case-sensitive. `blueprint=` and `setupblueprint=` are not recognized. + +##### Before you commit + +- Fence reads exactly `php interactive`. +- No `` on any HTML page per the [Playground guide](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). + +##### Timing and feedback + +The Code Reference is built from the current stable release, so an example in `trunk` appears when that release ships. + +Questions: `#live-doc-snippets` on Make WordPress Slack. Parser bugs: [phpdoc-parser](https://github.com/WordPress/phpdoc-parser/issues). Rendering bugs: [wporg-developer](https://github.com/WordPress/wporg-developer/issues). #### `@since` Section (Changelogs) diff --git a/inline-documentation-standards/runnable-examples.md b/inline-documentation-standards/runnable-examples.md deleted file mode 100644 index 2daab12..0000000 --- a/inline-documentation-standards/runnable-examples.md +++ /dev/null @@ -1,107 +0,0 @@ -# Runnable code examples in DocBlocks - -A DocBlock example can be marked as runnable. The Code Reference then shows it with a **Run** button, and readers execute and edit it in the browser through [WordPress Playground](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). - -This is opt-in. Existing indented examples keep working. Convert an example only when running it teaches something reading it does not. - -## Example -````php -/** - * Generator for a foreach loop to step through each class name for the matched tag. - * - * ```php interactive - * $p = new WP_HTML_Tag_Processor( "
" ); - * $p->next_tag(); - * foreach ( $p->class_list() as $class_name ) { - * echo "{$class_name} "; - * } - * // Outputs: free lang-en - * ``` - */ -```` - -Three parts: a fenced block, the info string `php interactive`, and a trailing `// Outputs:` comment. - -## The fence - -- `php interactive`, lowercase, in that order. Anything else (`PHP interactive`, `php-interactive`, `php title="x" interactive`) renders as plain code. -- May be indented, including inside a list item. Code lines need not match the fence indentation. -- Rendered in place, between the surrounding paragraphs. - -## The code - -Start with the first meaningful line. Do not include `' ); -// Outputs: -``` - -Several lines — empty `// Outputs:` starts a block, each `//` line is one output line, a final empty `//` keeps a trailing newline: -```php -print_r( array( 'fruit' => 'apple' ) ); -// Outputs: -// Array -// ( -// [fruit] => apple -// ) -// -``` - -JSON-encoded — for trailing spaces, tabs, or repeated newlines. Must be one JSON string: -```php -echo "done "; -// Outputs (JSON-encoded): "done " -``` - -Only a trailing standalone line comment counts. `// Outputs:` inside a string, a block comment, after code on the same line, or followed by more code is treated as code. The older ` ```expected-output ` fence still parses; do not use both. - -## Hidden setup - -If the example needs a helper function, a post, or an option, put that in a setup Blueprint ([Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) JSON). It runs first and never appears on the page. - -Inline, directly before or after the PHP fence: -```` -```setup-blueprint -{ "steps": [ { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/docs-fixture.php", "data": "`. A named Blueprint in the file-level DocBlock is available to every example in the file: -```` -```setup-blueprint shared-greeting -{ "steps": [ ... ] } -``` -```php interactive setup-blueprint=shared-greeting -echo docs_shared_greeting( 'first' ); -// Outputs: Hello, first -``` -```` - -Names are case-sensitive. `blueprint=` and `setupblueprint=` are not recognized. - -## Before you commit - -- Fence reads exactly `php interactive`. -- No `` on any HTML page per the [Playground guide](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). - -## Timing and feedback - -The Code Reference is built from the current stable release, so an example in `trunk` appears when that release ships. - -Questions: `#live-doc-snippets` on Make WordPress Slack. Parser bugs: [phpdoc-parser](https://github.com/WordPress/phpdoc-parser/issues). Rendering bugs: [wporg-developer](https://github.com/WordPress/wporg-developer/issues). diff --git a/manifest.json b/manifest.json index f6e7ae9..0fe8c6d 100644 --- a/manifest.json +++ b/manifest.json @@ -61,12 +61,6 @@ "parent": "inline-documentation-standards", "markdown_source": "https://github.com/WordPress/wpcs-docs/blob/master/inline-documentation-standards/php.md" }, - "inline-documentation-standards/runnable-examples": { - "title": "Runnable code examples in DocBlocks", - "slug": "runnable-examples", - "parent": "inline-documentation-standards", - "markdown_source": "https://github.com/WordPress/wpcs-docs/blob/master/inline-documentation-standards/runnable-examples.md" - }, "changelog": { "title": "Changelog", "slug": "changelog", From 6218131aebb6f440ca7ccf9f7813e7e79cb24e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 4 Sep 2026 13:04:24 +0200 Subject: [PATCH 4/8] Separate runnable example heading from code --- inline-documentation-standards/php.md | 1 + 1 file changed, 1 insertion(+) diff --git a/inline-documentation-standards/php.md b/inline-documentation-standards/php.md index da654db..aff10de 100644 --- a/inline-documentation-standards/php.md +++ b/inline-documentation-standards/php.md @@ -140,6 +140,7 @@ A DocBlock example can be marked as runnable. The Code Reference then shows it w This is opt-in. Existing indented examples keep working. Convert an example only when running it teaches something reading it does not. ##### Example + ````php /** * Generator for a foreach loop to step through each class name for the matched tag. From e487ba7cb8383cac666da0d96cf4fe206541c92d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 3 Nov 2026 20:16:38 +0400 Subject: [PATCH 5/8] Move runnable examples to their own page Reviewers asked for the section to leave the PHP Documentation Standards page: it put a long, optional topic ahead of the DocBlock tags every function needs, and a separate page can later cover JavaScript too. The text moves unchanged, with headings raised two levels. The PHP page links to it from the code samples item under Description and from the Functions & Class Methods list. --- inline-documentation-standards/php.md | 112 +----------------- .../runnable-code-examples.md | 108 +++++++++++++++++ manifest.json | 6 + 3 files changed, 117 insertions(+), 109 deletions(-) create mode 100644 inline-documentation-standards/runnable-code-examples.md diff --git a/inline-documentation-standards/php.md b/inline-documentation-standards/php.md index aff10de..38a8e2a 100644 --- a/inline-documentation-standards/php.md +++ b/inline-documentation-standards/php.md @@ -125,6 +125,8 @@ HTML markup should never be used outside of code examples, though Markdown can b * The description continues on ... ``` + To let readers run a code sample on the Code Reference, write it as a [runnable code example](https://developer.wordpress.org/coding-standards/inline-documentation-standards/runnable-code-examples/) instead. + 3. Links in the form of URLs, such as related Trac tickets or other documentation, should be added in the appropriate place in the DocBlock using the `@link` tag: ```php @@ -133,115 +135,6 @@ HTML markup should never be used outside of code examples, though Markdown can b * @link https://core.trac.wordpress.org/ticket/20000 ``` -#### Runnable examples - -A DocBlock example can be marked as runnable. The Code Reference then shows it with a **Run** button, and readers execute and edit it in the browser through [WordPress Playground](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). - -This is opt-in. Existing indented examples keep working. Convert an example only when running it teaches something reading it does not. - -##### Example - -````php -/** - * Generator for a foreach loop to step through each class name for the matched tag. - * - * ```php interactive - * $p = new WP_HTML_Tag_Processor( "
" ); - * $p->next_tag(); - * foreach ( $p->class_list() as $class_name ) { - * echo "{$class_name} "; - * } - * // Outputs: free lang-en - * ``` - */ -```` - -Three parts: a fenced block, the info string `php interactive`, and a trailing `// Outputs:` comment. - -##### The fence - -- `php interactive`, lowercase, in that order. Anything else (`PHP interactive`, `php-interactive`, `php title="x" interactive`) renders as plain code. -- May be indented, including inside a list item. Code lines need not match the fence indentation. -- Rendered in place, between the surrounding paragraphs. - -##### The code - -Start with the first meaningful line. Do not include `' ); -// Outputs: -``` - -Several lines — empty `// Outputs:` starts a block, each `//` line is one output line, a final empty `//` keeps a trailing newline: -```php -print_r( array( 'fruit' => 'apple' ) ); -// Outputs: -// Array -// ( -// [fruit] => apple -// ) -// -``` - -JSON-encoded — for trailing spaces, tabs, or repeated newlines. Must be one JSON string: -```php -echo "done "; -// Outputs (JSON-encoded): "done " -``` - -Only a trailing standalone line comment counts. `// Outputs:` inside a string, a block comment, after code on the same line, or followed by more code is treated as code. The older ` ```expected-output ` fence still parses; do not use both. - -##### Hidden setup - -If the example needs a helper function, a post, or an option, put that in a setup Blueprint ([Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) JSON). It runs first and never appears on the page. - -Inline, directly before or after the PHP fence: -```` -```setup-blueprint -{ "steps": [ { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/docs-fixture.php", "data": "`. A named Blueprint in the file-level DocBlock is available to every example in the file: -```` -```setup-blueprint shared-greeting -{ "steps": [ ... ] } -``` -```php interactive setup-blueprint=shared-greeting -echo docs_shared_greeting( 'first' ); -// Outputs: Hello, first -``` -```` - -Names are case-sensitive. `blueprint=` and `setupblueprint=` are not recognized. - -##### Before you commit - -- Fence reads exactly `php interactive`. -- No `` on any HTML page per the [Playground guide](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). - -##### Timing and feedback - -The Code Reference is built from the current stable release, so an example in `trunk` appears when that release ships. - -Questions: `#live-doc-snippets` on Make WordPress Slack. Parser bugs: [phpdoc-parser](https://github.com/WordPress/phpdoc-parser/issues). Rendering bugs: [wporg-developer](https://github.com/WordPress/wporg-developer/issues). - #### `@since` Section (Changelogs) Every function, hook, class, and method should have a corresponding `@since` version associated with it (more on that below). @@ -296,6 +189,7 @@ Functions and class methods should be formatted as follows: - **Summary**: A brief, one sentence explanation of the purpose of the function spanning a maximum of two lines. Use a period at the end. - **Description**: A supplement to the summary, providing a more detailed description. Use a period at the end of sentences. +- **Runnable code example**: Optional. A code sample in the description that readers can run on the Code Reference. See [Runnable Code Examples](https://developer.wordpress.org/coding-standards/inline-documentation-standards/runnable-code-examples/). - **`@ignore`**: Used when an element is meant only for internal use and should be skipped from parsing. - **`@since x.x.x`**: Should always be 3-digit (e.g. `@since 3.9.0`). Exception is `@since MU (3.0.0)`. - **`@access`**: Only used for core-only functions or classes implementing "private" core APIs. If the element is private it will be output with a message stating its intention for internal use. diff --git a/inline-documentation-standards/runnable-code-examples.md b/inline-documentation-standards/runnable-code-examples.md new file mode 100644 index 0000000..6b5d4ef --- /dev/null +++ b/inline-documentation-standards/runnable-code-examples.md @@ -0,0 +1,108 @@ +# Runnable Code Examples + +A DocBlock example can be marked as runnable. The Code Reference then shows it with a **Run** button, and readers execute and edit it in the browser through [WordPress Playground](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). + +This is opt-in. Existing indented examples keep working. Convert an example only when running it teaches something reading it does not. + +## Example + +````php +/** + * Generator for a foreach loop to step through each class name for the matched tag. + * + * ```php interactive + * $p = new WP_HTML_Tag_Processor( "
" ); + * $p->next_tag(); + * foreach ( $p->class_list() as $class_name ) { + * echo "{$class_name} "; + * } + * // Outputs: free lang-en + * ``` + */ +```` + +Three parts: a fenced block, the info string `php interactive`, and a trailing `// Outputs:` comment. + +## The fence + +- `php interactive`, lowercase, in that order. Anything else (`PHP interactive`, `php-interactive`, `php title="x" interactive`) renders as plain code. +- May be indented, including inside a list item. Code lines need not match the fence indentation. +- Rendered in place, between the surrounding paragraphs. + +## The code + +Start with the first meaningful line. Do not include `' ); +// Outputs: +``` + +Several lines — empty `// Outputs:` starts a block, each `//` line is one output line, a final empty `//` keeps a trailing newline: +```php +print_r( array( 'fruit' => 'apple' ) ); +// Outputs: +// Array +// ( +// [fruit] => apple +// ) +// +``` + +JSON-encoded — for trailing spaces, tabs, or repeated newlines. Must be one JSON string: +```php +echo "done "; +// Outputs (JSON-encoded): "done " +``` + +Only a trailing standalone line comment counts. `// Outputs:` inside a string, a block comment, after code on the same line, or followed by more code is treated as code. The older ` ```expected-output ` fence still parses; do not use both. + +## Hidden setup + +If the example needs a helper function, a post, or an option, put that in a setup Blueprint ([Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) JSON). It runs first and never appears on the page. + +Inline, directly before or after the PHP fence: +```` +```setup-blueprint +{ "steps": [ { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/docs-fixture.php", "data": "`. A named Blueprint in the file-level DocBlock is available to every example in the file: +```` +```setup-blueprint shared-greeting +{ "steps": [ ... ] } +``` +```php interactive setup-blueprint=shared-greeting +echo docs_shared_greeting( 'first' ); +// Outputs: Hello, first +``` +```` + +Names are case-sensitive. `blueprint=` and `setupblueprint=` are not recognized. + +## Before you commit + +- Fence reads exactly `php interactive`. +- No `` on any HTML page per the [Playground guide](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). + +## Timing and feedback + +The Code Reference is built from the current stable release, so an example in `trunk` appears when that release ships. + +Questions: `#live-doc-snippets` on Make WordPress Slack. Parser bugs: [phpdoc-parser](https://github.com/WordPress/phpdoc-parser/issues). Rendering bugs: [wporg-developer](https://github.com/WordPress/wporg-developer/issues). diff --git a/manifest.json b/manifest.json index 0fe8c6d..3871c6d 100644 --- a/manifest.json +++ b/manifest.json @@ -61,6 +61,12 @@ "parent": "inline-documentation-standards", "markdown_source": "https://github.com/WordPress/wpcs-docs/blob/master/inline-documentation-standards/php.md" }, + "inline-documentation-standards/runnable-code-examples": { + "title": "Runnable Code Examples", + "slug": "runnable-code-examples", + "parent": "inline-documentation-standards", + "markdown_source": "https://github.com/WordPress/wpcs-docs/blob/master/inline-documentation-standards/runnable-code-examples.md" + }, "changelog": { "title": "Changelog", "slug": "changelog", From 9f9d450aed9fa872d0f74225aa373900311d5845 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 3 Nov 2026 20:19:40 +0400 Subject: [PATCH 6/8] Revise runnable examples page after review Name the `// Outputs` comment the "output comment" and describe what readers see instead of what the parser does. The output comment is recommended but optional, matching phpdoc-parser, which stores no expected output when the comment is absent. Write every example as DocBlock lines, add incorrect and correct examples for output comment placement, and replace the setup Blueprint example, whose helper function could have been defined in the example itself, with user meta set by an `updateUserMeta` step. The main example becomes `sanitize_title()`: the previous `class_list()` example printed a trailing space that its single-line output comment could not state. State the setup Blueprint name rule the parser enforces (lowercase kebab-case starting with a letter), the scope of named setup Blueprints, and the metadata errors that stop the parser. Explain the `php interactive` info string with a link to the CommonMark spec. Remove the legacy `expected-output` fence, which is expected to be removed, and the timing and feedback section. Each example was checked against phpdoc-parser a1ba4bd's snippet extraction functions. --- .../runnable-code-examples.md | 205 ++++++++++++------ 1 file changed, 139 insertions(+), 66 deletions(-) diff --git a/inline-documentation-standards/runnable-code-examples.md b/inline-documentation-standards/runnable-code-examples.md index 6b5d4ef..3fb9567 100644 --- a/inline-documentation-standards/runnable-code-examples.md +++ b/inline-documentation-standards/runnable-code-examples.md @@ -1,108 +1,181 @@ # Runnable Code Examples -A DocBlock example can be marked as runnable. The Code Reference then shows it with a **Run** button, and readers execute and edit it in the browser through [WordPress Playground](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). +A code example in a PHP DocBlock can be made runnable by marking it `interactive`. On the [Code Reference](https://developer.wordpress.org/reference/), readers can edit a runnable example and run it with a **Run** button. [WordPress Playground](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/) runs the code in the browser. -This is opt-in. Existing indented examples keep working. Convert an example only when running it teaches something reading it does not. +Runnable examples are optional. Ordinary code samples, indented by four spaces as described in the [PHP Documentation Standards](https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#description), keep working. Convert a code sample only when running it teaches something that reading it cannot. ## Example ````php /** - * Generator for a foreach loop to step through each class name for the matched tag. + * Sanitizes a string into a slug, which can be used in URLs or HTML attributes. * * ```php interactive - * $p = new WP_HTML_Tag_Processor( "
" ); - * $p->next_tag(); - * foreach ( $p->class_list() as $class_name ) { - * echo "{$class_name} "; - * } - * // Outputs: free lang-en + * echo sanitize_title( 'Hello World!' ); + * // Outputs: hello-world * ``` + * + * @since 1.0.0 */ ```` -Three parts: a fenced block, the info string `php interactive`, and a trailing `// Outputs:` comment. +A runnable example has three parts: + +1. A fence that opens with `` ```php interactive `` and closes with `` ``` ``. +2. PHP code that prints something. +3. An output comment, starting with `// Outputs`, that states what the code prints. It is recommended but optional. ## The fence -- `php interactive`, lowercase, in that order. Anything else (`PHP interactive`, `php-interactive`, `php title="x" interactive`) renders as plain code. -- May be indented, including inside a list item. Code lines need not match the fence indentation. -- Rendered in place, between the surrounding paragraphs. +A fence is a Markdown code block that starts and ends with a line of three backticks. The text after the opening backticks is the fence's [info string](https://spec.commonmark.org/0.31.2/#info-string). Markdown uses its first word as the language. The second word, `interactive`, is a WordPress convention that the Code Reference recognizes. Markdown gives it no meaning. + +- The info string must be `php interactive`, lowercase, in that order. Any other info string, such as `PHP interactive`, `php-interactive`, or `php interactive title="x"`, displays as ordinary code that readers cannot run. +- The only other word allowed is a reference to a shared setup Blueprint, described under [Setup Blueprints](#setup-blueprints). +- The fence may be indented, for example inside a Markdown list item. Code lines may be indented less than the fence. +- The runnable example appears where the fence is, between the surrounding paragraphs. ## The code -Start with the first meaningful line. Do not include ` 'apple' ) ); + * // Outputs: + * // Array + * // ( + * // [fruit] => apple + * // ) + * // + * ``` +```` -## The output +### JSON-encoded output -End with a `// Outputs` comment. The parser strips it from the code that runs, shows it as the expected result before the reader selects **Run**, and keeps it for later output checks. +When the output contains tabs, trailing spaces, or several trailing newlines, write it as one JSON string after `// Outputs (JSON-encoded): `. -One line — everything after the colon is literal: -```php -echo esc_html( '' ); -// Outputs: -``` +````php + * ```php interactive + * echo implode( "\t", array( 'ID', 'Title' ) ); + * // Outputs (JSON-encoded): "ID\tTitle" + * ``` +```` -Several lines — empty `// Outputs:` starts a block, each `//` line is one output line, a final empty `//` keeps a trailing newline: -```php -print_r( array( 'fruit' => 'apple' ) ); -// Outputs: -// Array -// ( -// [fruit] => apple -// ) -// -``` +### Placement -JSON-encoded — for trailing spaces, tabs, or repeated newlines. Must be one JSON string: -```php -echo "done "; -// Outputs (JSON-encoded): "done " -``` +The output comment must be the last lines of the example, each comment on its own line. Otherwise the Code Reference treats it as code, and shows no output until the reader runs the example. -Only a trailing standalone line comment counts. `// Outputs:` inside a string, a block comment, after code on the same line, or followed by more code is treated as code. The older ` ```expected-output ` fence still parses; do not use both. +Incorrect, because the output comment shares a line with code: -## Hidden setup +````php + * ```php interactive + * echo sanitize_title( 'Hello World!' ); // Outputs: hello-world + * ``` +```` -If the example needs a helper function, a post, or an option, put that in a setup Blueprint ([Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) JSON). It runs first and never appears on the page. +Incorrect, because code follows the output comment: -Inline, directly before or after the PHP fence: +````php + * ```php interactive + * echo sanitize_title( 'Hello World!' ); + * // Outputs: hello-world + * echo sanitize_title( 'Goodbye, World!' ); + * ``` ```` -```setup-blueprint -{ "steps": [ { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/docs-fixture.php", "data": "`. A named Blueprint in the file-level DocBlock is available to every example in the file: +## Setup Blueprints + +Some examples need content or settings that a new WordPress site does not have, such as a user's first name. Code that creates them would distract from the function the example documents. Put that setup in a setup Blueprint instead: a [Playground Blueprint](https://developer.wordpress.org/playground/blueprints/) in a fence with the info string `setup-blueprint`. The Blueprint runs before the example, and the Code Reference does not show it. + +Place the setup Blueprint directly before or after the example's fence, with nothing but blank lines between them. Each example can have one setup Blueprint. + +````php + * ```setup-blueprint + * { "steps": [ { "step": "updateUserMeta", "userId": 1, "meta": { "first_name": "Ada" } } ] } + * ``` + * ```php interactive + * echo get_user_meta( 1, 'first_name', true ); + * // Outputs: Ada + * ``` ```` -```setup-blueprint shared-greeting -{ "steps": [ ... ] } -``` -```php interactive setup-blueprint=shared-greeting -echo docs_shared_greeting( 'first' ); -// Outputs: Hello, first -``` + +### Shared setup Blueprints + +To use one setup Blueprint in several examples, name it: add the name after `setup-blueprint` in its info string. Then add `setup-blueprint=` and the name after `php interactive` in each example that uses it. A name contains only lowercase letters, digits, and hyphens, and starts with a letter. + +A named setup Blueprint can be used in the DocBlock that defines it. One defined in a file header DocBlock can also be used in every DocBlock in the file, and one defined in a class DocBlock in the DocBlocks of the class's methods and properties. Do not define a name that the file header or class DocBlock already defines. + +In the class DocBlock: + +````php + * ```setup-blueprint user-ada + * { "steps": [ { "step": "updateUserMeta", "userId": 1, "meta": { "first_name": "Ada", "last_name": "Lovelace" } } ] } + * ``` ```` -Names are case-sensitive. `blueprint=` and `setupblueprint=` are not recognized. +In a method DocBlock: + +````php + * ```php interactive setup-blueprint=user-ada + * echo get_user_meta( 1, 'last_name', true ); + * // Outputs: Lovelace + * ``` +```` -## Before you commit +## Checklist -- Fence reads exactly `php interactive`. -- No `` on any HTML page per the [Playground guide](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). +- The opening fence reads `` ```php interactive ``, with `setup-blueprint=` after it only when the example uses a shared setup Blueprint. +- The code does not include `` element as described in the [Playground guide](https://developer.wordpress.org/playground/handbook/guides/php-code-snippets/). Add ` Date: Tue, 3 Nov 2026 20:44:32 +0400 Subject: [PATCH 7/8] Show runnable examples as complete DocBlocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each example on the page is now a complete PHP comment, with a `…snip…` line in place of the omitted description, so the page's PHP highlighting renders it correctly. The shared setup Blueprint example becomes one block, a class DocBlock defining the Blueprint and a method DocBlock using it, which shows where each part goes. --- .../runnable-code-examples.md | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/inline-documentation-standards/runnable-code-examples.md b/inline-documentation-standards/runnable-code-examples.md index 3fb9567..2961f78 100644 --- a/inline-documentation-standards/runnable-code-examples.md +++ b/inline-documentation-standards/runnable-code-examples.md @@ -17,6 +17,7 @@ Runnable examples are optional. Ordinary code samples, indented by four spaces a * * @since 1.0.0 */ +function sanitize_title() { /* …snip… */ } ```` A runnable example has three parts: @@ -56,10 +57,13 @@ The output can be a single line or multiline. Write the output after `// Outputs: ` on the same line. The rest of the line is the output, exactly as written. ````php +/** + * …snip… * ```php interactive * echo esc_html( 'Fish & Chips' ); * // Outputs: Fish & Chips * ``` + */ ```` ### Multiline output @@ -67,6 +71,8 @@ Write the output after `// Outputs: ` on the same line. The rest of the line is Write `// Outputs:` alone on a line, then one `//` line for each line of output. The first space after each `//` is removed. Any further spaces are part of the output. When the output ends with a newline, end the comment with an empty `//` line. ````php +/** + * …snip… * ```php interactive * print_r( array( 'fruit' => 'apple' ) ); * // Outputs: @@ -76,6 +82,7 @@ Write `// Outputs:` alone on a line, then one `//` line for each line of output. * // ) * // * ``` + */ ```` ### JSON-encoded output @@ -83,10 +90,13 @@ Write `// Outputs:` alone on a line, then one `//` line for each line of output. When the output contains tabs, trailing spaces, or several trailing newlines, write it as one JSON string after `// Outputs (JSON-encoded): `. ````php +/** + * …snip… * ```php interactive * echo implode( "\t", array( 'ID', 'Title' ) ); * // Outputs (JSON-encoded): "ID\tTitle" * ``` + */ ```` ### Placement @@ -96,24 +106,32 @@ The output comment must be the last lines of the example, each comment on its ow Incorrect, because the output comment shares a line with code: ````php +/** + * …snip… * ```php interactive * echo sanitize_title( 'Hello World!' ); // Outputs: hello-world * ``` + */ ```` Incorrect, because code follows the output comment: ````php +/** + * …snip… * ```php interactive * echo sanitize_title( 'Hello World!' ); * // Outputs: hello-world * echo sanitize_title( 'Goodbye, World!' ); * ``` + */ ```` Correct: ````php +/** + * …snip… * ```php interactive * echo sanitize_title( 'Hello World!' ), "\n"; * echo sanitize_title( 'Goodbye, World!' ); @@ -121,6 +139,7 @@ Correct: * // hello-world * // goodbye-world * ``` + */ ```` ## Setup Blueprints @@ -130,6 +149,8 @@ Some examples need content or settings that a new WordPress site does not have, Place the setup Blueprint directly before or after the example's fence, with nothing but blank lines between them. Each example can have one setup Blueprint. ````php +/** + * …snip… * ```setup-blueprint * { "steps": [ { "step": "updateUserMeta", "userId": 1, "meta": { "first_name": "Ada" } } ] } * ``` @@ -137,6 +158,7 @@ Place the setup Blueprint directly before or after the example's fence, with not * echo get_user_meta( 1, 'first_name', true ); * // Outputs: Ada * ``` + */ ```` ### Shared setup Blueprints @@ -145,21 +167,23 @@ To use one setup Blueprint in several examples, name it: add the name after `set A named setup Blueprint can be used in the DocBlock that defines it. One defined in a file header DocBlock can also be used in every DocBlock in the file, and one defined in a class DocBlock in the DocBlocks of the class's methods and properties. Do not define a name that the file header or class DocBlock already defines. -In the class DocBlock: - ````php +/** + * …snip… * ```setup-blueprint user-ada * { "steps": [ { "step": "updateUserMeta", "userId": 1, "meta": { "first_name": "Ada", "last_name": "Lovelace" } } ] } * ``` -```` - -In a method DocBlock: - -````php - * ```php interactive setup-blueprint=user-ada - * echo get_user_meta( 1, 'last_name', true ); - * // Outputs: Lovelace - * ``` + */ +class Example { + /** + * …snip… + * ```php interactive setup-blueprint=user-ada + * echo get_user_meta( 1, 'last_name', true ); + * // Outputs: Lovelace + * ``` + */ + public function example() { /* …snip… */ } +} ```` ## Checklist From a29407044bebbb86c25457e7611cdbd675f0f449 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 3 Nov 2026 20:46:10 +0400 Subject: [PATCH 8/8] Follow the Markdown style guide on the runnable examples page The style guide wraps inline code in single backticks, and no other page uses double-backtick code spans, so the two spans that showed a literal fence now name the info string instead. Two code spans ended in a space, which some Markdown parsers strip. The single-line output text now states the one space the parser removes after `// Outputs:`. The class example indents with four spaces, like the style guide's PHP sample and the PHP documentation pages. --- .../runnable-code-examples.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/inline-documentation-standards/runnable-code-examples.md b/inline-documentation-standards/runnable-code-examples.md index 2961f78..66e7675 100644 --- a/inline-documentation-standards/runnable-code-examples.md +++ b/inline-documentation-standards/runnable-code-examples.md @@ -22,7 +22,7 @@ function sanitize_title() { /* …snip… */ } A runnable example has three parts: -1. A fence that opens with `` ```php interactive `` and closes with `` ``` ``. +1. A fence with the info string `php interactive`. 2. PHP code that prints something. 3. An output comment, starting with `// Outputs`, that states what the code prints. It is recommended but optional. @@ -54,7 +54,7 @@ The output can be a single line or multiline. ### Single-line output -Write the output after `// Outputs: ` on the same line. The rest of the line is the output, exactly as written. +Write `// Outputs:`, one space, and the output on one line. Everything after that space is the output, exactly as written. ````php /** @@ -87,7 +87,7 @@ Write `// Outputs:` alone on a line, then one `//` line for each line of output. ### JSON-encoded output -When the output contains tabs, trailing spaces, or several trailing newlines, write it as one JSON string after `// Outputs (JSON-encoded): `. +When the output contains tabs, trailing spaces, or several trailing newlines, write it as one JSON string after `// Outputs (JSON-encoded):`. ````php /** @@ -165,7 +165,7 @@ Place the setup Blueprint directly before or after the example's fence, with not To use one setup Blueprint in several examples, name it: add the name after `setup-blueprint` in its info string. Then add `setup-blueprint=` and the name after `php interactive` in each example that uses it. A name contains only lowercase letters, digits, and hyphens, and starts with a letter. -A named setup Blueprint can be used in the DocBlock that defines it. One defined in a file header DocBlock can also be used in every DocBlock in the file, and one defined in a class DocBlock in the DocBlocks of the class's methods and properties. Do not define a name that the file header or class DocBlock already defines. +A named setup Blueprint can be used in the DocBlock that defines it. One defined in a file header DocBlock can also be used in every DocBlock in the file. One defined in a class DocBlock can also be used in the DocBlocks of the class's methods and properties. Do not define a name that the file header or class DocBlock already defines. ````php /** @@ -175,14 +175,14 @@ A named setup Blueprint can be used in the DocBlock that defines it. One defined * ``` */ class Example { - /** - * …snip… - * ```php interactive setup-blueprint=user-ada - * echo get_user_meta( 1, 'last_name', true ); - * // Outputs: Lovelace - * ``` - */ - public function example() { /* …snip… */ } + /** + * …snip… + * ```php interactive setup-blueprint=user-ada + * echo get_user_meta( 1, 'last_name', true ); + * // Outputs: Lovelace + * ``` + */ + public function example() { /* …snip… */ } } ```` @@ -190,7 +190,7 @@ class Example { Before committing a runnable example, check that: -- The opening fence reads `` ```php interactive ``, with `setup-blueprint=` after it only when the example uses a shared setup Blueprint. +- The fence's info string is `php interactive`, followed by `setup-blueprint=` only when the example uses a shared setup Blueprint. - The code does not include `