Disable the Gutenberg & Use Classic Editor Snippet for CPTs

Introduction

The Gutenberg editor is the default block editor introduced in WordPress version 5.0. While it offers a more modern and flexible editing experience, some users may prefer to use the classic editor for various reasons. In this guide, we will explain how to disable the Gutenberg editor and revert to the classic editor using code snippets. We will also provide examples of how to customize the code to disable Gutenberg for specific post types.

Prerequisites

Before proceeding with the steps outlined in this guide, you should have the following:

  • A WordPress website
  • Administrator access to the WordPress dashboard

The Code Snippets

The code snippets provided below disable the Gutenberg editor and enable the classic editor for all post types.

PHP
/**
 * Disable the Gutenberg editor and use the classic editor.
 */
function disable_gutenberg_editor() {
    return false;
}
add_filter( 'gutenberg_can_edit_post', 'disable_gutenberg_editor', 5 );
add_filter( 'use_block_editor_for_post', 'disable_gutenberg_editor', 5 );

Explanation

The code snippets provided above disable the Gutenberg editor and enable the classic editor for all post types. Let’s break down how it works:

  1. The disable_gutenberg_editor function is defined, which returns false. This function will be used to disable the Gutenberg editor and enable the classic editor.
  2. The add_filter function is used to hook into the gutenberg_can_edit_post and use_block_editor_for_post filters.
  3. The disable_gutenberg_editor function is passed as the callback to both add_filter functions. This means that whenever these filters are applied, the disable_gutenberg_editor function will be executed.
  4. By returning false from the disable_gutenberg_editor function, the Gutenberg editor is effectively disabled, and the classic editor will be used instead.

Customization: Disabling Gutenberg for Specific Post Types

If you want to disable the Gutenberg editor for specific post types (including custom post types), you can customize the code snippets. Here’s an example of how to disable Gutenberg for the “page” post type:

PHP
/**
 * Disable the Gutenberg editor and use the classic editor for the "page" post type.
 */
function disable_gutenberg_editor_for_page( $can_edit, $post_type ) {
    if ( 'page' === $post_type ) {
        return false;
    }
    return $can_edit;
}
add_filter( 'gutenberg_can_edit_post', 'disable_gutenberg_editor_for_page', 5, 2 );
add_filter( 'use_block_editor_for_post', 'disable_gutenberg_editor_for_page', 5, 2 );

In this example, we modified the disable_gutenberg_editor function to accept two parameters: $can_edit and $post_type. We then added a condition to check if the $post_type is “page”. If it is, we return false to disable Gutenberg for that specific post type.

You can customize this further by replacing 'page' with the desired post type slug. You can add multiple conditions to disable Gutenberg for multiple post types.

Conclusion

By following the steps outlined in this guide, you can easily disable the Gutenberg editor and revert to using the classic editor in WordPress. Whether you prefer the classic editor or need to maintain compatibility with certain plugins or themes, disabling Gutenberg provides you with the flexibility to choose the editing experience that suits your needs. Remember to regularly update WordPress and its plugins to ensure you are running the latest secure versions.

Customize Code

Try an AI WordPress Developer for Free

Do you need your code snippets to do something specific? Don’t worry you can use AI.


Email Icon

Get WP snippets in your inbox

Be one of the first to know about new PrimaryWP updates & code snippets for WordPress.

Newsletter

Leave a Reply

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