PHP code snippets and embeds
Use <php-snippet> when you want readers to run PHP or WordPress code directly
from a docs page, tutorial, blog post, or demo. It renders a syntax-highlighted
code block with a Run button and starts a real Playground runtime only when the
reader asks for it.
The runtime is shared across matching snippets on the same page, so a tutorial can include several runnable examples without starting WordPress over and over.
Try it
The example below is editable and runnable. It also uses a Blueprint to install a small mu-plugin before the snippet runs, so the PHP code can call a helper function that did not exist in the default WordPress install.
Here is the complete embed:
<script type="module" src="https://playground.wordpress.net/php-code-snippet.js"></script>
<script id="product-card-blueprint" type="application/json">
{
"steps": [
{
"step": "writeFile",
"path": "/wordpress/wp-content/mu-plugins/product-cards.php",
"data": "<?php\nfunction docs_render_product_card( array $product ): string {\n\treturn sprintf(\n\t\t'<article class=\"product-card\"><h3>%s</h3><p>$%0.2f</p></article>',\n\t\tesc_html( $product['name'] ),\n\t\t$product['price']\n\t);\n}\n"
}
]
}
</script>
<php-snippet name="product-card.php" blueprint="product-card-blueprint">
<script type="application/x-php">
<?php
require '/wordpress/wp-load.php';
$products = [
[
'name' => 'Canvas Tote',
'price' => 24,
],
[
'name' => 'Coffee & Code Mug',
'price' => 16.5,
],
];
foreach ( $products as $product ) {
echo docs_render_product_card( $product ) . "\n";
}
</script>
<script type="text/expected-output">
<article class="product-card"><h3>Canvas Tote</h3><p>$24.00</p></article>
<article class="product-card"><h3>Coffee & Code Mug</h3><p>$16.50</p></article>
</script>
</php-snippet>
Use this pattern when each example should start from the same prepared site: helper functions, mu-plugins, options, themes, demo files, or sample content.
Start with one snippet
For a basic runnable example, add the component script once and place PHP inside
<php-snippet>:
<script type="module" src="https://playground.wordpress.net/php-code-snippet.js"></script>
<php-snippet name="hello.php">
<script type="application/x-php">
<?php
echo 'Hello from PHP ' . phpversion();
</script>
<script type="text/expected-output">
Hello from PHP 8.4.x
</script>
</php-snippet>
The script itself is small. PHP, WordPress, and the WASM runtime are fetched later, after the first Run click. The expected output appears before Run and is replaced with the exact PHP version after execution.
Write PHP safely in HTML
Put inline PHP in a <script type="application/x-php"> child. Browsers ignore
script tags with unknown types, which means PHP strings can contain HTML without
escaping every < character.
<php-snippet name="html-api.php">
<script type="application/x-php">
<?php
require '/wordpress/wp-load.php';
$html = '<img src="hero.jpg" alt="Hero">';
$tags = new WP_HTML_Tag_Processor( $html );
if ( $tags->next_tag( 'img' ) ) {
$tags->set_attribute( 'loading', 'lazy' );
}
echo $tags->get_updated_html();
</script>
<script type="text/expected-output">
<img src="hero.jpg" alt="Hero" loading="lazy">
</script>
</php-snippet>
Very short snippets can also be written as text, as long as PHP opening tags are escaped:
<php-snippet name="sum.php" expected-output="42"> <?php echo 20 + 22; </php-snippet>
Use WordPress APIs
Snippets run in a real WordPress installation by default. Load WordPress with
require '/wordpress/wp-load.php', then call core APIs as usual.
<php-snippet name="site-title.php">
<script type="application/x-php">
<?php
require '/wordpress/wp-load.php';
update_option( 'blogname', 'Snippet Docs' );
echo get_bloginfo( 'name' );
</script>
<script type="text/expected-output">
Snippet Docs
</script>
</php-snippet>
If your example is pure PHP and does not need WordPress, use wp="none" to skip
the WordPress download and boot step:
<php-snippet name="pure-php.php" wp="none">
<script type="application/x-php">
<?php
echo 'WordPress installed: ';
echo file_exists( '/wordpress/wp-load.php' ) ? 'yes' : 'no';
</script>
<script type="text/expected-output">
WordPress installed: no
</script>
</php-snippet>
Edit examples in place
Runnable snippets are editable by default. The edited code is kept only in the current page session; refreshing restores the original snippet.
<php-snippet name="scratch.php">
<script type="application/x-php">
<?php
$numbers = range( 1, 5 );
echo array_sum( $numbers );
</script>
<script type="text/expected-output">
15
</script>
</php-snippet>
Editable snippets also run with Ctrl+Enter or Cmd+Enter while the editor is
focused.
Use readonly for runnable examples that should be copied or run as-is:
<php-snippet name="reference.php" readonly>
<script type="application/x-php">
<?php
echo 'This example can run, but the code is locked.';
</script>
<script type="text/expected-output">
This example can run, but the code is locked.
</script>
</php-snippet>
editable="false" works as a compatibility alias for readonly.