Make WordPress Core

Opened 6 weeks ago

Closed 6 weeks ago

#63122 closed defect (bug) (fixed)

Site Editor: Browser reload causes PHP warning error on the Pages screen

Reported by: wildworks's profile wildworks Owned by: peterwilsoncc's profile peterwilsoncc
Milestone: 6.8 Priority: normal
Severity: normal Version: trunk
Component: Editor Keywords: has-testing-info has-patch commit
Focuses: Cc:

Description

Warning: Undefined variable $post in /var/www/html/wp-admin/site-editor.php on line 242

Warning: Attempt to read property "post_name" on null in /var/www/html/wp-admin/site-editor.php on line 242

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-admin/site-editor.php:242) in /var/www/html/wp-admin/admin-header.php on line 14

This warning is new to 6.8.

Attachments (5)

Screenshot_1638.png (62.8 KB) - added by websiteredev 6 weeks ago.
PHP error on mobile viewport
Screenshot_1639.png (87.4 KB) - added by websiteredev 6 weeks ago.
PHP error hidden on desktop
Screenshot_1640.png (83.2 KB) - added by websiteredev 6 weeks ago.
Error on PHP 8.3.11
Screenshot_1641.png (85.8 KB) - added by websiteredev 6 weeks ago.
No error on PHP 7.4
Screenshot 2025-03-18 at 11.17.15 PM.png (739.3 KB) - added by narenin 6 weeks ago.
Screenshot of the error getting

Download all attachments as: .zip

Change History (23)

#1 @joemcgill
6 weeks ago

Hi @wildworks, I'm not able to reproduce this in trunk nor in a fresh install of WP 6.8 beta 3. Is there any other information that would be needed to reproduce this issue? Do you have any other plugins active that could be triggering this error?

@websiteredev
6 weeks ago

PHP error on mobile viewport

@websiteredev
6 weeks ago

PHP error hidden on desktop

#2 @websiteredev
6 weeks ago

@joemcgill I'm able to reproduce it here with an install upgraded from 6.7.2 to 6.8 using the Beta Tester Plugin.

Environment

  • WordPress: 6.8-beta3
  • PHP: 8.3.11
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.3.11)
  • Browser: Chrome 134.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Three 1.6
  • MU Plugins: None activated
  • Plugins:
    • Health Check & Troubleshooting 1.7.1
    • Test Reports 1.2.0
    • WordPress Beta Tester 3.6.2
    • WP Activity Log 5.3.3

#3 @wildworks
6 weeks ago

PHP error hidden on desktop

Yes, the warning text may be hard to see in desktop view, but it is there.

@websiteredev
6 weeks ago

Error on PHP 8.3.11

@websiteredev
6 weeks ago

No error on PHP 7.4

#4 @websiteredev
6 weeks ago

Unable to reproduce on an identical site that is on PHP 7.4, maybe PHP version based? What PHP version are you on @wildworks ?

#5 @wildworks
6 weeks ago

I'm on PHP 8.2.27. Not tested on PHP 7.4.

#6 @abcd95
6 weeks ago

Hi @wildworks thanks for bringing this up.

After investigating the issue, I found the problem is in site-editor.php. When building the URL for template lookup, there's a variable naming inconsistency:

empty( $block_editor_context->post->post_name ) ? 'page' : 'page-' . $post->post_name,

The code correctly uses $block_editor_context->post in the first part, but then incorrectly uses $post in the second part. Since $post is undefined at this point, it triggers PHP warnings when the browser is reloaded.

The fix is to use the same variable throughout:

empty( $block_editor_context->post->post_name ) ? 'page' : 'page-' . $block_editor_context->post->post_name,

This resolves the warning in my testing. Let me know if this works for you and I can raise a PR with the fix.

#7 @websiteredev
6 weeks ago

@abcd95 This fixes the issue in my testing.

This ticket was mentioned in PR #8535 on WordPress/wordpress-develop by @abcd95.


6 weeks ago
#8

  • Keywords has-patch added

#9 @narenin
6 weeks ago

@wildworks I'm also able to reproduce it here with in the given environment.

Environment

WordPress: 6.8-beta3-60042-src
PHP: 8.3.11
Server: nginx/1.26.1
Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.3.11)
Browser: Chrome 134.0.0.0
OS: Windows 10/11
Theme: Twenty Twenty-Five 1.1
MU Plugins: None activated

Plugins:
Health Check & Troubleshooting 1.7.1

Test Reports 1.2.1

@narenin
6 weeks ago

Screenshot of the error getting

#10 @narenin
6 weeks ago

  • Keywords has-testing-info added

Test Report :

Patch tested : https://github.com/WordPress/wordpress-develop/pull/8535

Environment

WordPress: 6.8-beta3-60042-src
PHP: 8.3.11
Server: nginx/1.26.1
Database: mysqli (Server: 8.0.35 / Client: mysqlnd 8.3.11)
Browser: Chrome 134.0.0.0
OS: Windows 10/11
Theme: Twenty Twenty-Five 1.1
MU Plugins: None activated

Plugins:
Health Check & Troubleshooting 1.7.1

Result:

After applying the patch the error has been gone.

Screenshot:

https://i.ibb.co/n8cyzGCM/Screenshot-2025-03-18-at-11-37-27-PM.png

@narenin commented on PR #8535:


6 weeks ago
#11

Thanks for the patch @himanshupathak95 !
This looks good to me, also with this patch the error/warning has been gone.

#12 @wildworks
6 weeks ago

@abcd95 Thanks for submitting the patch.

The code correctly uses $block_editor_context->post in the first part, but then incorrectly uses $post in the second part. Since $post is undefined at this point, it triggers PHP warnings when the browser is reloaded.

The fix is to use the same variable throughout:

This approach looks correct to me.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


6 weeks ago

#14 @soyebsalar
6 weeks ago

  • Keywords has-patch removed

Test Summary

This report verifies the resolution of a PHP warning in the WordPress Appearance Editor when accessing and reloading the Sample Page before and after applying the patch.
Test Environment

WordPress Version: 6.8 Beta 3
Server: Localhost (http://localhost/)
PHP Version: 8.1
Browser: chrome, firefox mozilla

Test Steps and Results
Before Applying Patch

Navigate to Appearance > Editor.
Click on Pages and select Sample Page.
The URL should be:

http://localhost/wp-admin/site-editor.php?p=%2Fpage&postId=2

Reload the browser.
Expected Result: The PHP warning should not appear.
Actual Result: The PHP warning appears.
Status: Failed.

After Applying Patch

Navigate to Appearance > Editor.
Click on Pages and select Sample Page.
Reload the browser.
Expected Result: No PHP warning should appear.
Actual Result: No PHP warning appears.
Status: Passed.

Conclusion

The applied patch successfully fixed the PHP warning in the WordPress 6.8 Beta 3 Appearance Editor. After applying the patch, reloading the Sample Page did not trigger any warnings.
Recommendation

Perform additional regression testing to ensure the patch does not introduce any unintended issues in other sections of the Appearance Editor.

#15 @soyebsalar
6 weeks ago

  • Keywords has-patch added

#16 @peterwilsoncc
6 weeks ago

  • Keywords commit added

I've tested the associated pull request and validated the fix:

  • warning no longer thrown on reload
  • REST request for /wp/v2/templates/lookup?slug=page-sample-page is correctly preloaded on the branch and no longer made upon page load

This looks good for commit, thank you!

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


6 weeks ago

#18 @peterwilsoncc
6 weeks ago

  • Owner set to peterwilsoncc
  • Resolution set to fixed
  • Status changed from new to closed

In 60061:

Site Editor: Fix PHP warning pre-loading page REST requests.

Fix a PHP warning when opening a page directly in the site editor, eg by navigating to the URL /wp-admin/site-editor.php?p=%2Fpage&postId=200, caused by an undefined variable name.

Props abcd95, joemcgill, narenin, soyebsalar, websiteredev, wildworks.
Fixes #63122.

Note: See TracTickets for help on using tickets.