Reusable "Preview in Playground" GitHub Workflow #2870
+1,138
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WordPress Playground Preview Button
Introduces a new, reusable GitHub workflow that automatically adds a "Try it in WordPress Playground" button to your pull requests.
When someone opens a pull request in your repository, this workflow:
This helps reviewers and contributors test your WordPress plugins and themes with a single click.
Requirements
pull_requesteventspull-requests: writeandcontents: readQuick start
First, figure out where your plugin or theme lives in your repository. This matters because the workflow needs to know what to install.
Step 1: Find your code
Open your repository and look at the file structure:
.phpfile with plugin headers right in the root (likemy-plugin.php), yourplugin-pathis.plugins/my-plugin/, yourplugin-pathisplugins/my-pluginstyle.csswith theme headers in the root, yourtheme-pathis.themes/my-theme/, yourtheme-pathisthemes/my-themeStep 2: Create the workflow file
Create a new file at
.github/workflows/pr-preview.ymlin your repository.For a plugin:
For a theme:
For both:
Step 3: Update the path
Change
plugin-pathortheme-pathto match where your code actually lives. Use what you found in Step 1.Step 4: Commit and push
Commit this new file to your repository. The next time someone opens a PR, the preview button will appear.
Not working?
Check the Actions tab in your repository. You'll see error messages there if something went wrong. Common issues:
style.cssHow to configure
Here's a basic config with the available inputs documented inline:
Both templates support variables using
{{VARIABLE_NAME}}syntax. Available variables:{{PLAYGROUND_BUTTON}}- The actual button HTML{{PLAYGROUND_URL}}- Direct link to the preview{{PR_NUMBER}}- Pull request number{{PR_TITLE}}- Pull request title{{PR_HEAD_REF}}- Branch name{{PR_HEAD_SHA}}- Latest commit{{REPO_OWNER}}- Repository owner{{REPO_NAME}}- Repository name{{PLUGIN_SLUG}}- Derived plugin slug{{THEME_SLUG}}- Derived theme slugAdvanced: Custom blueprints
Blueprints are JSON files that tell Playground exactly what to do. By default, the workflow creates a simple blueprint that installs your plugin or theme. But you can provide your own blueprint for more complex scenarios.
Common use cases for custom blueprints:
Here's how to create a custom blueprint:
This example installs your plugin plus WooCommerce, and uses WordPress 6.4 with PHP 8.3.
Learn more about blueprints in the [WordPress Playground documentation](https://wordpress.github.io/wordpress-playground/blueprints/).
Troubleshooting
When in trouble, always start with the "Actions" tab in your GitHub repository and check the log from the last time your workflow ran. You will likely fine useful error information in there. Here's a few common problems:
The workflow isn't running
Check that:
pull_requesteventsThe button isn't appearing
Check the Actions tab for error messages. Common issues:
plugin-path,theme-path, orblueprint)The preview doesn't load my code
Make sure:
plugin-path,theme-pathpoints to the right directorystyle.cssLearn more
Outputs
The workflow provides these outputs if you need them in subsequent jobs:
preview-url- The full Playground URLblueprint-json- The blueprint as JSONrendered-description- What gets added to PR descriptionsrendered-comment- What gets posted as a commentmode- Which mode was usedcomment-id- The comment ID (if using comment mode)FYI @fellyph @akirk @tyxla