Last active
October 21, 2025 16:55
-
-
Save lgladdy/1cafdd476b664162cc45632d3d60b64c to your computer and use it in GitHub Desktop.
A demo WordPress plugin of how you could disable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: ACF Blocks v3 Disable Preview | |
* Plugin URI: https://liamg.dev | |
* Description: Disables the preview/iframe rendering for ACF Blocks v3 that have mode support disabled and edit mode set. | |
* Version: 1.0.0 | |
* Author: Liam Gladdy | |
* Author URI: https://liamg.dev | |
* License: GPL v2 or later | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
* Text Domain: acf-blocks-v3-disable-preview | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
/** | |
* Intercept ACF block registration to replace render callback for v3 blocks with mode disabled. | |
* Runs AFTER ACF's own filter at priority 99. | |
* | |
* @param array $settings The block settings after ACF processing. | |
* @param array $metadata The raw block.json metadata. | |
* @return array Modified settings. | |
*/ | |
function acfbv3dp_replace_render_callback( $settings, $metadata ) { | |
// Only process ACF blocks | |
if ( ! isset( $metadata['acf'] ) || ! $metadata['acf'] ) { | |
return $settings; | |
} | |
// Check if this is a v3 block with mode support disabled | |
$is_v3 = isset( $metadata['acf']['blockVersion'] ) && $metadata['acf']['blockVersion'] >= 3; | |
$mode_disabled = isset( $settings['supports']['mode'] ) && $settings['supports']['mode'] === false; | |
$has_edit_mode = isset( $metadata['acf']['mode'] ) && $metadata['acf']['mode'] === 'edit'; | |
// If this is a v3 block with mode disabled and edit mode set, replace the callback | |
if ( $is_v3 && $mode_disabled && $has_edit_mode ) { | |
// ACF sets this to 'acf_render_block_callback' at line 160 of blocks.php | |
// We override it here to use our custom callback instead | |
$settings['render_callback'] = 'acfbv3dp_no_preview_callback'; | |
} | |
return $settings; | |
} | |
add_filter( 'block_type_metadata_settings', 'acfbv3dp_replace_render_callback', 98, 2 ); | |
/** | |
* Custom render callback for blocks that don't support preview mode. | |
* | |
* Returns ACF's special 'acf-block-preview-no-html' value in the editor to trigger | |
* the NoBlockPreviewPlaceholder component with the "Edit Block" button. | |
* | |
* @param array $block The block settings and attributes. | |
* @param string $content The block content. | |
* @param bool $is_preview Whether or not the block is being rendered for editing preview. | |
* @param int $post_id The current post ID. | |
* @param WP_Block $wp_block The block instance. | |
* @param array $context The block context. | |
* @return string Special value for ACF or empty string. | |
*/ | |
function acfbv3dp_no_preview_callback( $block, $content = '', $is_preview = false, $post_id = 0, $wp_block = null, $context = array() ) { | |
// If we're in preview mode, return ACF's special value | |
// This triggers the NoBlockPreviewPlaceholder React component with "Edit Block" button | |
if ( $is_preview ) { | |
echo 'acf-block-preview-no-html'; | |
} else { | |
// On the frontend, call ACF's original render function | |
if ( function_exists( 'acf_render_block_callback' ) ) { | |
acf_render_block_callback( $block, $content, $is_preview, $post_id, $wp_block, $context ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment