Attempt to read property “post_type” on null in .php on line 327
-
First and foremost, I apologize for using the error message as the title of this topic. My intention was to make it easier for others who might encounter the same issue in the future to find this thread via search engines like Google. I have already tried searching for a solution to this problem but couldn’t find any relevant results, so I decided to seek help here.
I encountered the following error message when I installed the “PublishPress Permissions” plugin. Specifically, I noticed the error on the PublishPress Permissions settings page and when trying to create a new post, page, or document. In some cases, this issue also leads to a change in the document editor to the block editor.
Warning: Attempt to read property "post_type" on null in class-wp-document-revisions-admin.php on line 327
Upon inspecting the PHP file in the wp document revisions plugin, I found the following code snippet:
[325] public function no_use_block_editor( $use_block_editor, $post ) { [326] // switch off for documents. [327] if ( 'document' === $post->post_type || $this->verify_post_type( $post ) ) { [328] return false; [329] } [330] return $use_block_editor; [331] }
The error indicates that on line 327, there is an attempt to read the “post_type” property on the variable $post, but the variable is null. To address this, I attempted to ensure that $post has a valid value before trying to read the post_type property, and the following code modification helped resolve the error:
public function no_use_block_editor( $use_block_editor, $post ) { // switch off for documents. if ( !empty($post) && ('document' === $post->post_type || $this->verify_post_type( $post )) ) { return false; } return $use_block_editor; }
With this change, the error is no longer present. However, I’m aware that modifying the plugin’s code directly can cause issues when the plugin is updated in the future. To avoid this problem, I tried to override the “no_use_block_editor” function within the plugin using the functions.php file in my theme.
add_filter( 'use_block_editor_for_post', 'custom_no_use_block_editor', 10, 2 ); function custom_no_use_block_editor( $use_block_editor, $post ) { // switch off for documents. if ( !empty($post) && ('document' === $post->post_type || $this->verify_post_type( $post )) ) { return false; } return $use_block_editor; }
Unfortunately, this approach didn’t work, and I would greatly appreciate the WP Document Revisions team’s guidance on achieving this successfully.
I recognize that I am not yet very skilled in using PHP and WordPress, I’m seeking your expertise to help me find a proper solution to this problem. I am grateful for your kind support and guidance!
Best regards,
Mifta Widaya
- The topic ‘Attempt to read property “post_type” on null in .php on line 327’ is closed to new replies.