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.
/**
* 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:
- The
disable_gutenberg_editor
function is defined, which returnsfalse
. This function will be used to disable the Gutenberg editor and enable the classic editor. - The
add_filter
function is used to hook into thegutenberg_can_edit_post
anduse_block_editor_for_post
filters. - The
disable_gutenberg_editor
function is passed as the callback to bothadd_filter
functions. This means that whenever these filters are applied, thedisable_gutenberg_editor
function will be executed. - By returning
false
from thedisable_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:
/**
* 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.
Leave a Reply