Blueprints: Define constants in auto_prepend_file, silence warnings related to redefining those constants #1400
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.
Changes the default meethod of defining PHP constants from rewriting
the
define()
calls in wp-config.php to pre-defining constants viaan
auto_prepend_file
.Problem solved by this PR
Playground needs to define some WordPress constants like SITE_URL or HOME_URL.
Unfortunately, the site loaded into Playground may already have a
conflicting
define()
call in itswp-config.php
. Historically, we'vaavoided PHP warnings by rewriting the
define()
calls in thewp-config.php
file.It works quite well, but when the
wp-config.php
file is mounted from alocal directory, Playground would change its contents. This is typically
not what the user wants. These changes would show up in
git commit
andwould be annoying at best, or get accidentally commited to the
repository at worst.
Implementation
Instead of rewriting the
wp-config.php
, this PR changes the defaultbehavior to defining the constants in a pre-loaded PHP file. This
triggers PHP warnings, but we're silencing them using a custom
error handler. As a result, Playground constants take precedence
over custom define() calls in a way that doesn't trigger warnings.
This is an easy and hacky way of implementing this. A better solution
would be a PHP.wasm-level patch to enable "warning-less constants" that
can't be redefined but the conflicting
define()
call fails silently.Testing instructions
Confirm the unit tests pass