Automattic / gutenberg-ramp

Control conditions under which Gutenberg loads - either from your theme code or from a UI
https://wordpress.org/plugins/gutenberg-ramp/
76 stars 15 forks source link

"Greater Than" #78

Open chriscoyier opened 5 years ago

chriscoyier commented 5 years ago

It's nice that we can provide ID's of particular posts to enable Gutenburg on, but I would think in the spirit of "ramping", it would be cool to enable Gutenberg on any post/page/post_type GREATER than a certain ID (or date?). Legacy content gets old editor, new content gets Gutenberg.

Is this possible now? Or would the params that gutenberg_ramp_load_gutenberg takes have to change?

chriscoyier commented 4 years ago

I have a monkey patch for this I could submit a PR for if there is any interest.

chriscoyier commented 4 years ago

I wanted to be able to link to the code so here's a PR for it.

https://github.com/Automattic/gutenberg-ramp/pull/82

pyronaur commented 4 years ago

Hey @chriscoyier!

I think the best long-term solution for everyone would be to use Classic Editor over Gutenberg Ramp.

WordPress 5.0 introduced a filter use_block_editor_for_post that allows to change whether or not Block Editor (Gutenberg) should be enabled for any given post.

I actually wrote a blog post about this 🙂 - https://pyronaur.com/how-to-dynamically-enable-gutenberg

The tldr; is - you can use that filter to enable Gutenberg for new posts. Here's a snippet from the post that will enable Gutenberg for new posts and posts that have blocks in them:

function maybe_enable_gutenberg( $can_edit, $post ) {

    // -- For new posts
    // Check for `/wp-admin/post-new.php`
    $current = get_current_screen();
    if ( 'post' === $current->base && 'add' === $current->action ) {
        // For new posts,
        // If using Classic Editor - return whatever the current default for the site is
        return $can_edit;
    }

    // -- For existing posts

    // Enable Gutenberg in posts with empty post content
    if ( empty( $post->post_content ) ) {
        return $can_edit;
    }

    // Determine whether Gutenberg should be
    // enabled based on whether the post content has blocks
    return has_blocks( $post );
}

// Classic Editor is using hook priority 100
add_filter( 'use_block_editor_for_post', 'maybe_enable_gutenberg', 200, 2 );

The snippet above should work in WordPress 5.0+ with and without Classic Editor and Gutenberg plugins. However - if the site should support Classic Editor for years to come - the Classic Editor plugin should probably be installed, as I don't know if/when Classic Editor will be removed from WordPress core.

With all that said, I think that to enable Gutenberg for post IDs above a certain number, it would be easier to just use WordPress Core hooks:

function over_9000( $can_edit, $post ) {
    return ( $post->ID > 9000 );
}
add_filter( 'use_block_editor_for_post', 'over_9000', 10, 2 );
chriscoyier commented 4 years ago

Oh wow - yeah essentially a one-liner there instead of an entire monkeypatched plugin seems like a way nicer approach, thanks.