Repository navigation
Load WordPress before interactive PHP snippets - #579
Conversation
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.
|
@sirreal I've implemented the Playground part and used What do you think? |
|
Love it @adamziel |
…tion/block.php Co-authored-by: Jon Surrell <sirreal@users.noreply.github.com>
|
Yay! The related Playground PR landed, I'll hold on with deploying playground.wordpress.net until WCUS ends though—it seems fine for our purposes here. |
|
I tested and it seems like the feature is live. I'll do some final testing but I think this can land. |
|
This doesn't work for me. I see the global bootstrap script being added to the block content but it's not present in the final, served HTML. I'm debugging. |
|
Table of contents renders the content - the script tag was produced correctly - but this render is thrown away because the ToC is not interested in all the content, only headings. Then the content is actually rendered but the static variable indicates the script tag was already rendered, so it's not produced in the content that's actually rendered to the page. |
The code reference blocks render twice per request: once when the table of contents block applies `wporg_table_of_contents_post_content` to collect headings, whose output is discarded, and once for `the_content`. The static once-per-request guard fired on the first pass, so the shared auto-prepend script never reached the page. Register a named `wp_footer` callback instead. Repeat registration is a no-op, so the script prints exactly once per page regardless of how many times the block renders. The Playground side resolves `auto-prepend-script` when a snippet runs, so footer placement is fine.
|
Fixed in 69dd573. The block now registers a named Now with the committed 7.1 including
|
WordPress 7.1 ships two `php interactive` examples, both for `class_list()`, that open with `<?php` and `require '/wordpress/wp-load.php';` because the runtime had no other way to load WordPress for them. With `implicit-php-open-tag` the runtime rejects code that starts with an opening tag, so those examples would stop running. Remove that exact preamble before encoding the snippet. The match is the two lines as shipped, with no blank line after the `require`; any other lead-in is left alone. Drop this once the examples are parsed without the preamble.
|
d13131d will strip the WordPress load code from snippets. This is working across the 7.1 and 7.2+ expected syntax. Here's this working on the existing site with the 7.1 docs: demo.movAnd here it is running against a 7.2+ expected snippet based on WordPress/wordpress-develop#13267: demo2.movBefore this change, the current snippets looked like this, including the
This seems ready, I'll plan to merge soon. |

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#4262:
wp_footeron each reference page that renders snippets:+jsonscript type, matching how the block already emits snippet code and expected output.JSON_HEX_TAGkeeps<escaped so the payload stays inert in HTML. The code reference blocks render twice per request (once for the table of contents, once for the content), so the script is printed from a namedwp_footercallback rather than inline; repeat registration is a no-op.auto-prepend-script="#wporg-code-snippet-auto-prepend"andimplicit-php-open-tagon every<php-snippet>the code-description block emits, including placeholder-placed and appended-fallback snippets.<?php/require '/wordpress/wp-load.php';preamble from the twoclass_list()examples that shipped in WordPress 7.1, so they keep running underimplicit-php-open-tag. Docs: Simplify interactive docs sample wordpress-develop#13267 removes the preamble from the docblocks themselves; the strip goes once the parser strips it or Core ships without it.auto-prepend-scriptmirrors PHP'sauto_prepend_filephp.inidirective while taking a selector for an inert HTML script element. The selected script is a complete PHP file with its own opening tag. Parser-provided examples omit the opening tag, soimplicit-php-open-tagadds it without displaying it in the editor. The shared ID is safe because the script is emitted only once per page.Depends on WordPress/wordpress-playground#4262, deployed to playground.wordpress.net.
The Playground-handbook import path (
inc/import-playground.php) is 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-auto-prependscript, in the footer; every<php-snippet>carries the matchingauto-prepend-scriptattribute andimplicit-php-open-tag; and examples run with WordPress loaded while displaying no opening tag orwp-load.phpboilerplate.