Skip to content

Load WordPress before interactive PHP snippets - #579

Merged
sirreal merged 11 commits into
trunkfrom
add/php-snippet-wordpress-bootstrap
Aug 26, 2026
Merged

sirreal merged 11 commits into
trunkfrom
add/php-snippet-wordpress-bootstrap

Conversation

@sirreal

@sirreal sirreal commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

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:

  • Prints one shared auto-prepend element from wp_footer on each reference page that renders snippets:
    <script id="wporg-code-snippet-auto-prepend" type="application/x-php+json">"<?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 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 named wp_footer callback rather than inline; repeat registration is a no-op.
  • Stamps auto-prepend-script="#wporg-code-snippet-auto-prepend" and implicit-php-open-tag on every <php-snippet> the code-description block emits, including placeholder-placed and appended-fallback snippets.
  • Strips the <?php / require '/wordpress/wp-load.php'; preamble from the two class_list() examples that shipped in WordPress 7.1, so they keep running under implicit-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-script mirrors PHP's auto_prepend_file php.ini directive 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, so implicit-php-open-tag adds 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-prepend script, in the footer; every <php-snippet> carries the matching auto-prepend-script attribute and implicit-php-open-tag; and examples run with WordPress loaded while displaying no opening tag or wp-load.php boilerplate.

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
sirreal requested a review from adamziel August 14, 2026 11:16
@adamziel adamziel changed the title Bootstrap WordPress for interactive PHP snippets Load WordPress before interactive PHP snippets Aug 14, 2026
@adamziel

Copy link
Copy Markdown
Contributor

@sirreal I've implemented the Playground part and used auto-prepend-script as the attribute name to mirror the auto_prepend_file php.ini setting. I've also added a implicit-php-open-tag boolean attribute and enabled it by default in the WP.org theme. When present, you may skip the opening <?php from the snippet. I also took the liberty of updating this PR.

What do you think?

@sirreal

sirreal commented Aug 15, 2026

Copy link
Copy Markdown
Member Author

Love it @adamziel

…tion/block.php

Co-authored-by: Jon Surrell <sirreal@users.noreply.github.com>
@adamziel

Copy link
Copy Markdown
Contributor

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.

@sirreal

sirreal commented Aug 18, 2026

Copy link
Copy Markdown
Member Author

I tested and it seems like the feature is live.

I'll do some final testing but I think this can land.

@sirreal

sirreal commented Aug 21, 2026

Copy link
Copy Markdown
Member Author

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.

@sirreal

sirreal commented Aug 21, 2026

Copy link
Copy Markdown
Member Author

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.
@sirreal

sirreal commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

Fixed in 69dd573.

The block now registers a named wp_footer callback instead of printing inline. The script is rendered once in the page where necessary.

Now with the committed 7.1 including <?php require…, we'll need to strip that. Otherwise we get this warning when running the snippet:

<php-snippet implicit-php-open-tag> code must not start with a PHP opening tag.

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.
@sirreal

sirreal commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

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.mov

And here it is running against a 7.2+ expected snippet based on WordPress/wordpress-develop#13267:

demo2.mov

Before this change, the current snippets looked like this, including the <?php require '…'; inside the snippet text:

Screenshot 2026-08-25 at 16 31 32

This seems ready, I'll plan to merge soon.

@sirreal
sirreal merged commit 38f06a7 into trunk Aug 26, 2026
1 check passed
@sirreal
sirreal deleted the add/php-snippet-wordpress-bootstrap branch August 26, 2026 04:35
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