WordPress.org

Make WordPress Core

Opened 11 months ago

Closed 9 months ago

#47198 closed defect (bug) (duplicate)

Block editor for posts page can only be loaded when post content is not empty

Reported by: gchtr Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.2
Component: Editor Keywords:
Focuses: Cc:

Description

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
// 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

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

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:

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.

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?

Change History (1)

#1 @swissspidy
9 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #45537.

See also #45263.

Note: See TracTickets for help on using tickets.