Skip to content

Live snippets: Load WordPress implicitly - #580

Closed
adamziel wants to merge 5 commits into
trunkfrom
add/php-snippet-fragments
Closed

adamziel wants to merge 5 commits into
trunkfrom
add/php-snippet-fragments

Conversation

@adamziel

@adamziel adamziel commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Readers of Code Reference examples should see and edit only the code that documents the API—not the Playground-specific require '/wordpress/wp-load.php'; boilerplate—while still running examples with WordPress loaded, including after editing them.

This wires the theme side of WordPress/wordpress-playground#4261:

  • Emits one shared run-before element per reference page that renders snippets, alongside the existing setup Blueprint scripts:
    <script id="wporg-code-snippet-run-before" type="application/x-php+json">"\u003C?php require_once '/wordpress/wp-load.php';"</script>
    The payload is JSON-encoded with the +json script type, matching how the block already emits snippet code and expected output. JSON_HEX_TAG keeps < escaped so the payload remains inert in HTML.
  • Stamps run-before="#wporg-code-snippet-run-before" and php-fragment on every <php-snippet> emitted by the code-description block, including placeholder-placed and appended-fallback snippets.

The shared script is a complete PHP file with its own opening tag. Parser-provided examples remain fragments, so php-fragment adds their opening tag without displaying it in the editor.

This depends on WordPress/wordpress-playground#4262 and should land after that component change.

The Playground-handbook import path (inc/import-playground.php) remains untouched. Those embeds reproduce Playground documentation examples authored for their own execution context.

Testing

Import parser data containing interactive snippets, open a Code Reference page, and confirm the page source contains exactly one wporg-code-snippet-run-before script; every <php-snippet> carries the matching run-before attribute and php-fragment; and examples run with WordPress loaded while displaying no opening tag or wp-load.php boilerplate.

sirreal and others added 5 commits August 14, 2026 13:17
The bootstrap payload is identical for every snippet on every post, so
keying its script ID by post ID served no purpose. Use one global ID and
render the tag only on the first call, so a page with several code
descriptions still holds a single element with that ID.
@adamziel adamziel changed the title Render parsed PHP examples as fragments Run parsed PHP fragments with WordPress loaded Aug 14, 2026
@adamziel
adamziel changed the base branch from add/php-snippet-wordpress-bootstrap to trunk August 14, 2026 13:04
@adamziel
adamziel requested a review from sirreal August 14, 2026 18:44
@adamziel adamziel changed the title Run parsed PHP fragments with WordPress loaded Live snippets: Load WordPress implicitly Aug 14, 2026
@adamziel

Copy link
Copy Markdown
Contributor Author

Closing in favor of #579, which now includes the run-before and php-fragment updates.

@adamziel adamziel closed this Aug 14, 2026
@sirreal
sirreal deleted the add/php-snippet-fragments branch August 17, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants