PHP Warning Message
-
Hi guys,
I’m seeing a repeating warning being logged in my hosting portal. I pasted the error message into Claude and uploaded the plugin zip. It came up with a reasoning and solution for the error.
I asked Claude to write a post that I can copy/paste for you detailing the issue:
PHP Warning: Attempt to read property “post_type” on null — display.php line 312Version: 4.2.4
Seeing the following in error logs:
PHP Warning: Attempt to read property "post_type" on null in /widget-options/includes/widgets/display.php on line 312The issue is in the
is_single()branch (~line 307). When the global$postis empty, the code callsget_post()as a fallback but doesn’t null-check the result before accessing->post_type:if (!$post) { $current_post = get_post(); $type = $current_post->post_type; // Warning if get_post() returns null }get_post()can return null in certain contexts (REST API requests, some AJAX calls) whereis_single()is true but no post object is available.Suggested fix:
if (!$post) { $current_post = get_post(); $type = $current_post ? $current_post->post_type : ''; }Not a critical issue in terms of security, but it can cause widgets configured to show on specific post types to be incorrectly hidden when
get_post()returns null. Also generates log noise and will surface as an error ifdisplay_errorsis on.
You must be logged in to reply to this topic.