Detecting per-post use of Gutenberg

There may be times when you need to detect use of Gutenberg on a per-post level – for example, a plugin that adds specific Gutenberg functionality, or maybe to display specific content on Gutenberg pages.

With plugins such as Gutenberg Ramp available, making it easy to specify specific groups of posts that should use Gutenberg, plus the fact that only posts you edit/add after installing Gutenberg will make use of its new functionality, it’s easy to see that most sites will end up with a mixed estate.

The following simple function will add a simple is_gutenberg() function that you can call to check the status of the current post (I leave it to you to check for whether it’s a single post, etc).  It returns either a true or false result.

<?php
function is_gutenberg() {
global $post;
if ( function_exists( 'gutenberg_post_has_blocks' ) && gutenberg_post_has_blocks( $post->ID ) {
return true;
} else {
return false;
}
}
view raw functions.php hosted with ❤ by GitHub

Discover more from artiss.blog

Subscribe to get the latest posts sent to your email.

Comments

2 responses to “Detecting per-post use of Gutenberg”

  1. Amanda Giles avatar

    Thank you so much for this! Just a heads up though – you’re missing the closing parenthesis on the if statement and gutenberg_post_has_blocks() has now been deprecated in favor of has_blocks().

  2. Christoph avatar

    Thank you!

    I found that has_blocks() – previously gutenberg_post_has_blocks() – does not reliably detect Gutenberg when you create a new post (in WP 5.0.2). That’s because the post is still empty, while has_blocks() checks for the substring ‘<!– wp:' https://github.com/WordPress/gutenberg/blob/master/lib/register.php

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.