Code Monkey home page Code Monkey logo

Comments (4)

pyronaur avatar pyronaur commented on September 27, 2024 1

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 );

from gutenberg-ramp.

chriscoyier avatar chriscoyier commented on September 27, 2024

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

from gutenberg-ramp.

chriscoyier avatar chriscoyier commented on September 27, 2024

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

#82

from gutenberg-ramp.

chriscoyier avatar chriscoyier commented on September 27, 2024

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

from gutenberg-ramp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.