Disable Gutenberg Editor on some ACF pages

Disable Gutenberg Editor on some ACF pages

I’ve had a few people ask about the code snippet that disables gutenberg builder for the pages with an ACF backend.

I had originally used a code snippet in the plugin directly with the ids added to the snippet, but decided I should just make it a separate section on the backend to make it easier to work with.

The Video

Timestamps

  • 00:00 Intro
  • 00:30 Using the custom plugin
  • 1:00 the php code
  • 3:25 How to use it

The idea is simple, just add an options page via ACF and then make a repeater field with number values.

Take the ids entered into the fields, and put them into an array and disable the default editor on the pages with the corresponding ids. (use snippet 1 + 2)

It works best for pages like home, about us, etc. If you’d like it to work on all decendant pages as well, just use snippets 1 + 3.

If you need a place to paste your code into, you can either use a custom plugin like the one below, a child theme functions.php file, or a code snippet plugin like WP codebox.

Extra Code

Here's the code used in the episode. Click on 'copy code' to copy to your clipboard and paste it appropriately.
Type of code: php

This snippet adds the backend options page where you can add page ids to disable the gutenberg editor. This php should go in either your plugin, theme functions file, or in a code snippets plugin.

add_action( 'acf/include_fields', function() {
	if ( ! function_exists( 'acf_add_local_field_group' ) ) {
		return;
	}

	acf_add_local_field_group( array(
	'key' => 'group_67c074a03cc21',
	'title' => 'Extra Options',
	'fields' => array(
		array(
			'key' => 'field_67c0fad9761eb',
			'label' => 'Hide editor',
			'name' => 'hide_editor',
			'aria-label' => '',
			'type' => 'repeater',
			'instructions' => '',
			'required' => 0,
			'conditional_logic' => 0,
			'wrapper' => array(
				'width' => '',
				'class' => '',
				'id' => '',
			),
			'layout' => 'table',
			'pagination' => 0,
			'min' => 0,
			'max' => 0,
			'collapsed' => '',
			'button_label' => 'Add Row',
			'rows_per_page' => 20,
			'sub_fields' => array(
				array(
					'key' => 'field_67c10a319591a',
					'label' => 'Page id',
					'name' => 'page_id',
					'aria-label' => '',
					'type' => 'number',
					'instructions' => '',
					'required' => 0,
					'conditional_logic' => 0,
					'wrapper' => array(
						'width' => '',
						'class' => '',
						'id' => '',
					),
					'default_value' => '',
					'min' => '',
					'max' => '',
					'allow_in_bindings' => 0,
					'placeholder' => '',
					'step' => '',
					'prepend' => '',
					'append' => '',
					'parent_repeater' => 'field_67c0fad9761eb',
				),
			),
		),
	),
	'location' => array(
		array(
			array(
				'param' => 'options_page',
				'operator' => '==',
				'value' => 'site-global-settings',
			),
		),
	),
	'menu_order' => 2,
	'position' => 'normal',
	'style' => 'seamless',
	'label_placement' => 'top',
	'instruction_placement' => 'label',
	'hide_on_screen' => '',
	'active' => true,
	'description' => '',
	'show_in_rest' => 0,
) );
} );

add_action( 'acf/init', function() {
	acf_add_options_page( array(
	'page_title' => 'Site Global Settings',
	'menu_slug' => 'site-global-settings',
	'position' => '',
	'redirect' => false,
	'menu_icon' => array(
		'type' => 'dashicons',
		'value' => 'dashicons-buddicons-topics',
	),
	'icon_url' => 'dashicons-buddicons-topics',
) );
} );
Type of code: php

This snippet does the disabling part. This php should go in either your plugin, theme functions file, or in a code snippets plugin.

function ai_disable_editor_for_acf_pages($use_block_editor, $post) {
    $disabled_page_ids = get_field('hide_editor', 'option') ?: [];
    
    $page_ids = array_map(function($item) {
        return intval($item['page_id']);
    }, $disabled_page_ids);

    if (in_array($post->ID, $page_ids)) {
        return false;
    }

    return $use_block_editor;
}
add_filter('use_block_editor_for_post', 'ai_disable_editor_for_acf_pages', 10, 2);

function ai_disable_classic_editor_for_acf_pages() {
    global $pagenow;

    if ($pagenow !== 'post.php') {
        return;
    }

    $disabled_page_ids = get_field('hide_editor', 'option') ?: [];
    
    $page_ids = array_map(function($item) {
        return intval($item['page_id']);
    }, $disabled_page_ids);

    $post_id = isset($_GET['post']) ? intval($_GET['post']) : 0;

    if (in_array($post_id, $page_ids)) {
        remove_post_type_support('page', 'editor');
    }
}
add_action('admin_init', 'ai_disable_classic_editor_for_acf_pages');
Type of code: php

NOTE: Use this instead of the one above if you'd like all the decendants to have disabled editors also.

function ai_disable_editor_for_acf_pages($use_block_editor, $post) {
    $disabled_page_ids = get_field('hide_editor', 'option') ?: [];
    $page_ids = array_map(function($item) {
        return intval($item['page_id']);
    }, $disabled_page_ids);

    $ancestors = get_post_ancestors($post->ID);

    if ( in_array($post->ID, $page_ids) || !empty(array_intersect($ancestors, $page_ids)) ) {
        return false;
    }

    return $use_block_editor;
}
add_filter('use_block_editor_for_post', 'ai_disable_editor_for_acf_pages', 10, 2);

function ai_disable_classic_editor_for_acf_pages() {
    global $pagenow;

    if ($pagenow !== 'post.php') {
        return;
    }

    $disabled_page_ids = get_field('hide_editor', 'option') ?: [];
    $page_ids = array_map(function($item) {
        return intval($item['page_id']);
    }, $disabled_page_ids);

    $post_id = isset($_GET['post']) ? intval($_GET['post']) : 0;
    if (!$post_id) {
        return;
    }

    $ancestors = get_post_ancestors($post_id);

    if ( in_array($post_id, $page_ids) || !empty(array_intersect($ancestors, $page_ids)) ) {
        remove_post_type_support('page', 'editor');
    }
}
add_action('admin_init', 'ai_disable_classic_editor_for_acf_pages');

Get Breakdance

Breakdance is, in my opinion, the best page/site builder on the market, which is why all my tutorials use Breakdance. It's also why I have an affiliate link. You don't pay extra, but I get a little commission, so it helps support the site. Thanks!

Get ACF Pro

Another essential plugin for site building. This isn't an affiliate link, it's just for convenience.

Get WP Codebox Pro

If you'd like an easier to use, and more advanced code management plugin (instead of just editing and making files yourself) then WP codebox pro is for you. Adding php, js, and even SCSS. Pretty nice! Affiliate link below.

Blank Plugin

Sort of blank. It's made to be easy to use with the tutorials. Download and edit any way you see fit.
    Leave a Reply

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

    New tutorials

    Auto-updating Year in Footer
    Designing Beyond the Brief
    Disable Gutenberg Editor on some ACF pages
    ACF flexible content fields in Breakdance 2.4

    Got a question? ask!

    If you have any question about the content on this page, feel free to send me an email by clicking the button below.

    newsletter? sign up!

    Sign up to the newsletter for updates on tutorials, news, and offers.

    Join the cypher!

    An awesome membership program for Breakdance Builder users coupled with an inclusive, friendly community.

    Got a question?

    If you have a question about something on this page, send me an email, and hopefully I can answer, and we can solve it!