#59723 closed defect (bug) (fixed)
Pattern file containing PHP constant from theme causes fatal error on update to WordPress 6.4
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.4 | Priority: | high |
| Severity: | normal | Version: | 6.4 |
| Component: | Upgrade/Install | Keywords: | has-patch has-test-info commit dev-reviewed fixed-major |
| Focuses: | Cc: |
Description
I have a theme that sets up some PHP constants in its functions.php file. These constants for example reference the dist directory of the built assets.
<?php define( 'NAMESPACE_THEME_TEMPLATE_URL', get_template_directory_uri() ); define( 'NAMESPACE_THEME_DIST_URL', NAMESPACE_THEME_TEMPLATE_URL . '/dist/' );
This theme then also contains some patterns in the patterns directory.
Some of these patterns reference one of the globals defined in the functions.php file to load an image from the dist folder.
<?php <?php /** * Title: Hero - Default * Slug: namespace/hero-default * Categories: header * Viewport Width: 500 */ ?> <!-- wp:cover {"url":"<?php echo esc_url( NAMESPACE_THEME_DIST_URL . 'images/placeholder/pattern--no-text.jpg' ); ?>","dimRatio":70,"minHeight":70,"minHeightUnit":"vh","contentPosition":"bottom left","isDark":false,"align":"full"} -->
This works great when just using the site. But when installing WordPress 6.4 RC1 or RC2 via CLI the update appears to run through but when I try to log into the Admin I get a blank white screen on the URL: /wp-admin/upgrade.php
When I look at the debug.log file I get a fatal error saying that the constant used in the pattern file isn't defined.
So it appears the patterns are getting loaded before the theme is loaded.
Change History (33)
This ticket was mentioned in โSlack in #core by fabiankaegy. โView the logs.
2 years ago
#3
@
2 years ago
- Milestone changed from Awaiting Review to 6.4
- Owner set to joemcgill
- Priority changed from normal to high
- Status changed from new to reviewing
#4
@
2 years ago
- Milestone changed from 6.4 to Awaiting Review
- Priority changed from high to normal
I can replicate the error. If we use the theme shared, we get the below error.
PHP Fatal error: Uncaught Error: Undefined constant "NAMESPACE_IMAGE_URL" in ..\public\wp-content\themes\wp-core-59723-sample-theme-main\patterns\sample.php:9
#5
@
2 years ago
Also, I am able to replicate it, when I am trying to downgrade from 6.4 to 6.3.2. It seems that this is calling the patterns before the theme itself, whenever the DB update is required after an update.
Specifically, on this page: โhttp://my-site.localhost/wp-admin/upgrade.php?_wp_http_referer=%2Fwp-admin%2F
#6
@
2 years ago
- Keywords needs-patch added
- Milestone changed from Awaiting Review to 6.4
- Priority changed from normal to high
- Status changed from reviewing to accepted
Thanks, @fabiankaegy. It looks like what is happening here is that the first time wp_get_active_and_valid_themes() is run one the upgrade page, via โthis line, the function returns an empty array, which means the functions file isn't included.
Patterns are registered on init and do not include a check for whether the functions file has already been loaded, but we should probably add an extra check here, so patterns aren't loaded whenever wp_installing() returns true.
This ticket was mentioned in โPR #5568 on โWordPress/wordpress-develop by โ@joemcgill.
2 years ago
#7
- Keywords has-patch added; needs-patch removed
This fixes a bug on upgrade screens where constants defined in a theme's functions.php file are not defined before the patterns are loaded, leading to potential exceptions.
Trac ticket: https://core.trac.wordpress.org/ticket/59723
#8
follow-up:
โย 12
@
2 years ago
Just a side query, why are we loading the patterns on the init instead of after_theme_setup? ๐ง
The patterns are a part of that theme after all.
#9
@
2 years ago
After more investigation, it looks like this issue existed prior to the 6.4 release cycle, and was previously reported in #59020. It's possible this issue extends all the way back to WP version 6.0, when this was first introduced in [53152].
I'll close that ticket as a duplicate if we decide to move forward with this fix.
@rajinsharwar, to answer your question:
Just a side query, why are we loading the patterns on the init instead of after_theme_setup?
It's a good question...I'm not sure what the original rationale was for choosing that hook. However, it this case it wouldn't matter because the after_theme_setup hook fires before init in wp-settings.php and the functions.php file would still not be loaded in this case, leading to the same error.
#10
follow-up:
โย 24
@
2 years ago
Steps to reproduce
- start w/ a fresh site running 6.3.2
- install/activate the sample theme in comment:2
- update to 6.4-RC2 via WP_CLI,
wp core update --version=6.4-RC - check the PHP error logs, and notice
PHP Fatal error: Uncaught Error: Undefined constant "NAMESPACE_IMAGE_URL" in path-to-site/wp-content/themes/wp-core-59723-sample-theme-main/patterns/sample.php:9
Steps to test the โPR
- roll the site back to 6.3.2,
wp core update --version=6.3.2 --force- note: another error will be logged, you can ignore it the purposes of this testing
- update
wp-includes/block-patterns.phpin the WP_CLI ZIP file in~/.wp-cli/cache/core/wordpress-6.4-RC2-en_US.zipso that the PR has been applied (i.e., add the early return from_register_theme_block_patterns()ifwp_is_installing()is true) - clear your PHP error log file
- update to 6.4-RC2,
wp core update --version=6.4-RC2 - check the PHP error logs and notice no error was logged
This ticket was mentioned in โSlack in #core by pbiron. โView the logs.
2 years ago
#12
in reply to:
โย 8
@
2 years ago
Replying to rajinsharwar:
Just a side query, why are we loading the patterns on the init instead of after_theme_setup? ๐ง
The patterns are a part of that theme after all.
I have the same question. Registering a theme's pattens on after_theme_setup (w/ a priority of 20) seems to make much more sense. Maybe there's some underlying reason the editor needs them on init?
#13
@
2 years ago
I can reproduce error. following comment:10
PHP Fatal error: Uncaught Error: Undefined constant "NAMESPACE_IMAGE_URL" in /Users/huzaifa/Project/wp/test/wp-content/themes/wp-core-59723-sample-theme/patterns/sample.php:9
This ticket was mentioned in โSlack in #core by hellofromtonya. โView the logs.
2 years ago
#15
@
2 years ago
- Keywords changes-requested needs-testing has-testing-info added
Updating the keywords:
Patch: โhttps://github.com/WordPress/wordpress-develop/pull/5568
needs-testingis testing the patch, once it's ready.has-testing-info: found in comment:10changes-requested: discussion is happening in the patch of possible changes.
Alerted contributors in the โMake/Core core channel with a call for testing and review.
Cross-sharing here:
๐ Should it go into 6.4 RC3?
IMO yes, fatals should be fixed. But there must be high confidence that the patch does indeed resolve the fatal without side effects.
"high confidence" comes from code reviews and test reports. The more test reports, the higher the confidence.
This ticket was mentioned in โSlack in #core by hellofromtonya. โView the logs.
2 years ago
This ticket was mentioned in โSlack in #core-upgrade-install by pbiron. โView the logs.
2 years ago
This ticket was mentioned in โSlack in #core by pbiron. โView the logs.
2 years ago
โ@pbiron commented on โPR #5568:
2 years ago
#19
Other than the minor changes requested in the reviews I just posted, the PR and tests LGTM.
โ@joemcgill commented on โPR #5568:
2 years ago
#20
Thanks @pbiron. These are great suggestions, which I've applied in โd8f3501.
#21
@
2 years ago
@fabiankaegy if you have a chance, can you please test out the latest PR and see if it resolves the issue for you?
โ@pbiron commented on โPR #5568:
2 years ago
#22
I've confirmed that this fixes the bug and that the tests are testing what they intend to test and pass w/ TT3.
For future consideration: we might consider adding a test theme whose patterns assume that functions.php has been loaded and use that theme in the tests.
โ@pbiron commented on โPR #5568:
2 years ago
#23
The PHPUnit Tests / PHP 7.4 / PHP 7.4 / MariaDB 10.11 multisite (pull_request) test run errored out. Looks unrelated to this PR.
@joemcgill Are you able to restart the unit tests?
#24
in reply to:
โย 10
@
2 years ago
Replying to pbiron:
Steps to reproduce
- start w/ a fresh site running 6.3.2
- install/activate the sample theme in comment:2
- update to 6.4-RC2 via WP_CLI,
wp core update --version=6.4-RC- check the PHP error logs, and notice
PHP Fatal error: Uncaught Error: Undefined constant "NAMESPACE_IMAGE_URL" in path-to-site/wp-content/themes/wp-core-59723-sample-theme-main/patterns/sample.php:9Steps to test the โPR
- roll the site back to 6.3.2,
wp core update --version=6.3.2 --force
- note: another error will be logged, you can ignore it the purposes of this testing
- update
wp-includes/block-patterns.phpin the WP_CLI ZIP file in~/.wp-cli/cache/core/wordpress-6.4-RC2-en_US.zipso that the PR has been applied (i.e., add the early return from_register_theme_block_patterns()ifwp_is_installing()is true)- clear your PHP error log file
- update to 6.4-RC2,
wp core update --version=6.4-RC2- check the PHP error logs and notice no error was logged
Following these test steps I am also no longer able to reproduce the issue :)
#25
@
2 years ago
- Keywords changes-requested removed
I've cleaned up the unit tests for this fix to avoid looping through all of the theme patterns. I would like to get a +1 from another committer (cc: @peterwilsoncc or @hellofromTonya) before committing this to trunk, and then will need that commit dev-reviewed in order to include this in the 6.4 branch.
In the meantime, I'd be happy for others to post the results of their testing, so we can demonstrate a high level of confidence (thanks, @pbiron and @fabiankaegy).
#27
@
2 years ago
- Keywords commit dev-reviewed added; needs-testing removed
Patch: โhttps://github.com/WordPress/wordpress-develop/pull/5568
- Multiple approvals on the PR โ
- Multiple test reports the PR resolves the issue โ
- Confidence is high โ (though more test reports are encouraged and welcomed)
Marking the patch for commit and, to expedite, dev-reviewed for backport to 6.4 branch.
#29
@
2 years ago
- Keywords fixed-major added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening for the back porting to the 6.4 branch once tests have passed, given @hellofromTonya has already marked this as dev-reviewed.
I have created a sample repository containing a minimal theme setup to reproduce the issue: โhttps://github.com/fabiankaegy/wp-core-59723-sample-theme