Categories
Pro Tips

Enabling Gutenberg-Based Custom Post Types and Taxonomies

Codeable.io

In the ever-evolving landscape of WordPress, Gutenberg has brought a significant shift in how content is created and managed. Its block-based approach offers a more intuitive and versatile way of building pages and posts. However, there are scenarios where you might want to enable Gutenberg selectively, based on specific Custom Post Types (CPTs) and Taxonomies. Krasen Slavov has crafted a solution that does precisely this, enabling Gutenberg for specific content types, in this case, for ‘Resources > Infographics’.

Why Enable Gutenberg Selectively?

Gutenberg’s block editor is a powerful tool, but it might not always be necessary or beneficial for all types of content. In certain cases, the classic editor might be more suitable, or you might want to limit Gutenberg’s use to specific areas of your site for consistency or ease of management.

Enabling Gutenberg for Specific Custom Post Types and Taxonomies

The function created by Krasen Slavov allows you to control where Gutenberg is enabled based on both the post type and its taxonomy. Here’s how the function works:

add_filter( 'use_block_editor_for_post_type', 'wt_enable_gutenberg', 10, 2 );

function wt_enable_gutenberg( $current_status, $post_type ) {
    global $post;

    $tax = (array) get_the_terms( $post->ID, 'resource_type' );

    if ( 'resource_item' === $post_type
        && in_array( 'infographics', array_column( $tax, 'slug' ), true ) ) {
        return true;
    }

    return false;
}

Understanding the Function

  • Hook into Gutenberg: The function hooks into the use_block_editor_for_post_type filter.
  • Check Post Type and Taxonomy: It then checks if the post type is ‘resource_item’ and if it has the ‘infographics’ taxonomy.
  • Enable Gutenberg: If both conditions are met, Gutenberg is enabled for that post.
  • Flexibility: This approach offers flexibility, allowing you to enable the block editor for specific content types while keeping it disabled for others.

Use Cases

This function is particularly useful in scenarios such as:

  • Content-Specific Editor Needs: Where certain types of content benefit from the block editor’s features, while others do not.
  • Gradual Integration: Gradually introducing Gutenberg to your workflow by enabling it for specific sections of your site.
  • Complex Websites: Websites with a variety of content types, where a one-size-fits-all approach to content editing doesn’t work.

Implementing the Function

To use this in your WordPress site:

  1. Insert the Code: Place the above function in your theme’s functions.php file.
  2. Customize: Modify the post type and taxonomy slugs as per your needs.

The ability to selectively enable Gutenberg based on Custom Post Types and Taxonomies offers a level of customization and control that can significantly enhance the content management process on your WordPress site. By implementing such targeted solutions, you can leverage Gutenberg’s strengths where they are most needed while maintaining a traditional editing experience where it’s more appropriate.

Remember, as with any modifications to your WordPress site, testing in a staging environment is recommended before applying changes to your live site.

Leave a Reply

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