• Resolved MIFTA WIDAYA

    (@miftawidaya)


    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

    https://prnt.sc/smS9YwzeDudt

    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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor nwjames

    (@nwjames)

    @miftawidaya,

    I believe that you should have the line as

        if ( ( ! empty($post) && 'document' === $post->post_type ) || $this->verify_post_type( $post ) ) { 

    or just

        if ( $this->verify_post_type( $post ) ) {

    I would be grateful if you could try the second one. I will make a proposal to change it to that text.

    Regards,

    Neil James

    Thread Starter MIFTA WIDAYA

    (@miftawidaya)

    @nwjames ,

    Thank you for your response and suggestions. I tried the second approach by modifying the plugin file, and I can confirm that it resolved the issue. Now, I no longer encounter any problems when creating posts, pages, and documents.

    However, I also attempted various methods to override the function through the functions.php file in my theme, but unfortunately, none of them worked as expected. I’m still trying to understand the concept behind the “verify_post_type()” function and how it works. I searched for documentation regarding this function, but couldn’t find any relevant information.

    Given my limited understanding of PHP OOP, I’m unsure if it’s possible to successfully override this function through the functions.php file. If it is indeed possible, I would greatly appreciate any guidance or examples on how to achieve this correctly.

    Once again, thank you for your assistance and prompt response.

    Best regards,
    Miftawidaya

    Plugin Contributor nwjames

    (@nwjames)

    @miftawidaya

    Thank you for confirming that it resolves the problem.

    I am suggesting that you just edit the plugin code to have this new line.

    I understand that you are concerned that this will be overwritten if or when a new version comes out.

    I would say that Ben Balter (the plugin owner) has generally accepted the changes that I propose – and that I will be proposing this change to be made for the next version.

    Given that, there is no need for you to go to look for methods to work around the issue, Just edit the plugin code. When a new version is delivered, review it to see if the fix is done. If it is, nothing else to do, otherwise re-edit the code.

    Your alternative when a new version is delivered would require you to review it to see if the fix is done. If it is, remove the bypass, otherwise nothing else to do. Of course, you first need to create the bypass in the first place.

    For me, the first option is the least effort and most reliable option.

    Hope this clarifies,

    Neil James

    Thread Starter MIFTA WIDAYA

    (@miftawidaya)

    @nwjames,

    Thank you for your guidance and advice.

    Your explanation about the two alternatives for handling this issue—directly editing the plugin code or creating a bypass—is very helpful. I understand that making the direct edit is likely the simpler and more reliable option, especially since you have a plan to integrate the fix into the next plugin release.

    I’ll take your advice and follow the path of editing the plugin code for now. This way, I can ensure that the issue is resolved and keep an eye out for the fix in the upcoming version. Your clear explanation has given me a better understanding of how to approach this situation.

    Thank you once again for your support and the clarification you’ve provided.

    Best regards,
    Mifta Widaya

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Attempt to read property “post_type” on null in .php on line 327’ is closed to new replies.