'Disable Gutenberg editor for certain page ids
I need to disable the gutenberg editor on the edit screen of certain pages.
I know how to disable it for post types, like all pages:
/************************************************************ */
/* Disable Gutenberg for post type */
add_filter('use_block_editor_for_post_type', 'prefix_disable_gutenberg', 10, 2);
function prefix_disable_gutenberg($current_status, $post_type)
{
if ( $post_type == 'page' ) return false;
return $current_status;
}
But I only want to disable it for a page, let's say with the ID of "1716".
How can I disable the gutenberg editor for certain page IDs programmatically?
Solution 1:[1]
It's possible with the use_block_editor_for_post
hook.
You can do it a variety of ways then with the hook, but here's one example;
modify the excluded_ids array with post_ids that you want edited by the classic-editor.(remember $post is applicable regardless across all post types)
function disable_block_editor_for_page_ids( $use_block_editor, $post ) {
$excluded_ids = array( 2374, 5378, 21091);
if ( in_array( $post->ID, $excluded_ids ) ) {
return false;
}
return $use_block_editor;
}
add_filter( 'use_block_editor_for_post', 'disable_block_editor_for_page_ids', 10, 2 );
Note that you must have the classic-editor plugin installed for this to work.
Solution 2:[2]
You can also do it this way:
add_filter( 'use_block_editor_for_post', 'disable_gutenberg', 10, 2 );
function disable_gutenberg( $can_edit, $post ) {
if( $post->ID == '223' ) {
return false;
}
return true;
}
Gutenberg is disabled when it returns 'false', 223 is page id. but it can give you some problems if you are using acf. It's better to use it this way.
add_filter( 'use_block_editor_for_post', 'disable_gutenberg', 10, 2 );
function disable_gutenberg( $can_edit, $post ) {
if( $post->post_type == 'page' && $post->ID == '223' ) {
return true;
}
return false;
}
It uses Gutenberg only on page witch's id is 223. And disables it on all other pages.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |