[wp-trac] [WordPress Trac] #47198: Block editor for posts page can only be loaded when post content is not empty

WordPress Trac noreply at wordpress.org
Thu May 9 09:43:40 UTC 2019


#47198: Block editor for posts page can only be loaded when post content is not
empty
--------------------------+-----------------------------
 Reporter:  gchtr         |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Editor        |    Version:  5.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 In https://core.trac.wordpress.org/changeset/43883/branches/5.0/src/wp-
 admin/includes/post.php, a change was introduced that restricts the block
 editor from being loaded on the `page_for_posts` page, when the post
 content is empty:

 {{{#!php
 <?php
 // The posts page can't be edited in the block editor.
 if ( absint( get_option( 'page_for_posts' ) ) === $post->ID && empty(
 $post->post_content ) ) {
     return false;
 }
 }}}

 I wanted to enable the block editor for the posts page, so that I can make
 it possible to insert content before the posts list on the posts page.

 For this, I use a custom filter to enable editing the content of the posts
 page:

 {{{#!php
 <?php

 add_action( 'add_meta_boxes_page', 'allow_post_content_for_posts_page' );

 /**
  * Re-enables post content for posts page and removes admin notice.
  *
  * Since WordPress 4.2, if you edit the page used as the posts page and
 there’s no content
  * saved for that page, the editor functionality is removed and a message
 displayed instead.
  *
  * @link https://gist.github.com/benhuson/cd1fc54edbe53eef4b60
  *
  * @param \WP_Post $post Post object.
  */
 function allow_post_content_for_posts_page( $post ) {
     if ( get_option( 'page_for_posts' ) !== $post->ID ) {
         return;
     }

     // Optionally remove notice that informs users they’re editing the
 posts page.
     remove_action( 'edit_form_after_title', '_wp_posts_page_notice' );

     // Enable editor.
     add_post_type_support( get_post_type( $post ), 'editor' );
 }
 }}}

 Additionally, I use another filter to enable the blocks editor for the
 posts page:

 {{{#!php
 <?php

 add_filter( 'use_block_editor_for_post',
 'allow_block_editor_for_posts_page', 10, 2 );

 /**
  * Enable block editor for page_for_posts.
  *
  * The block editor is disabled for the posts page by default. This filter
 makes sure it is
  * enabled.
  *
  * @see use_block_editor_for_post()
  *
  * @param bool     $use_block_editor Whether the post can be edited or
 not.
  * @param \WP_Post $post             The post being checked.
  * @return bool Whether the block editor is enabled for a post.
  */
 function allow_block_editor_for_posts_page( $use_block_editor, $post ) {
     if ( get_option( 'page_for_posts' ) === $post->ID ) {
         return true;
     }

     return $use_block_editor;
 }
 }}}

 Now, when I load the edit screen for the posts page, I see a content box,
 where I can set my content:

 [[Image(https://i.imgur.com/wAePIfq.png)]]

 As soon as I save, the page reloads with Gutenberg and a classic editor
 block with my content. Just as I expect with the current code.

 [[Image(https://i.imgur.com/O60NZdK.png)]]

 When I remove the block and save again, the classic editor loads when I
 reload the page.

 I see no way to change this behaviour. Is there a reason that would
 strongly forbid to load the block editor for an empty posts page? Or can
 this be considered a bug?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47198>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list