函数文档

do_block_editor_incompatible_meta_box()

💡 云策文档标注

概述

do_block_editor_incompatible_meta_box() 函数用于在块编辑器中检测到不兼容的元框时,渲染一个包含信息提示的“伪”元框。它根据插件状态和用户权限,提供安装、激活 Classic Editor 插件或切换到经典编辑器的建议。

关键要点

  • 函数接受两个参数:$data_object(渲染的数据对象)和 $box(元框参数数组,包括 id、title、old_callback 和 args)。
  • 通过 _get_plugin_from_callback() 获取生成元框的插件信息,并显示相应的不兼容提示消息。
  • 根据 Classic Editor 插件的状态(未安装、未激活或已激活)和用户权限(如 install_plugins、activate_plugins),生成安装、激活链接或经典编辑器切换链接。
  • 使用 WordPress 核心函数如 get_plugins()、is_plugin_inactive()、wp_nonce_url() 等来处理插件和 URL 操作。

代码示例

function do_block_editor_incompatible_meta_box( $data_object, $box ) {
	$plugin  = _get_plugin_from_callback( $box['old_callback'] );
	$plugins = get_plugins();
	echo '';
	if ( $plugin ) {
		/* translators: %s: The name of the plugin that generated this meta box. */
		printf( __( 'This meta box, from the %s plugin, is not compatible with the block editor.' ), "{$plugin['Name']}" );
	} else {
		_e( 'This meta box is not compatible with the block editor.' );
	}
	echo '';

	if ( empty( $plugins['classic-editor/classic-editor.php'] ) ) {
		if ( current_user_can( 'install_plugins' ) ) {
			$install_url = wp_nonce_url(
				self_admin_url( 'plugin-install.php?tab=favorites&user;=wordpressdotorg&save;=0' ),
				'save_wporg_username_' . get_current_user_id()
			);

			echo '';
			/* translators: %s: A link to install the Classic Editor plugin. */
			printf( __( 'Please install the Classic Editor plugin to use this meta box.' ), esc_url( $install_url ) );
			echo '';
		}
	} elseif ( is_plugin_inactive( 'classic-editor/classic-editor.php' ) ) {
		if ( current_user_can( 'activate_plugins' ) ) {
			$activate_url = wp_nonce_url(
				self_admin_url( 'plugins.php?action=activate&plugin;=classic-editor/classic-editor.php' ),
				'activate-plugin_classic-editor/classic-editor.php'
			);

			echo '';
			/* translators: %s: A link to activate the Classic Editor plugin. */
			printf( __( 'Please activate the Classic Editor plugin to use this meta box.' ), esc_url( $activate_url ) );
			echo '';
		}
	} elseif ( $data_object instanceof WP_Post ) {
		$edit_url = add_query_arg(
			array(
				'classic-editor'         => '',
				'classic-editor__forget' => '',
			),
			get_edit_post_link( $data_object )
		);
		echo '';
		/* translators: %s: A link to use the Classic Editor plugin. */
		printf( __( 'Please open the classic editor to use this meta box.' ), esc_url( $edit_url ) );
		echo '';
	}
}

注意事项

  • 此函数自 WordPress 5.0.0 版本引入,主要用于处理块编辑器与旧版元框的兼容性问题。
  • 依赖于 _get_plugin_from_callback() 等内部辅助函数,开发者应确保在调用前相关函数可用。
  • 消息输出使用 __() 和 _e() 进行国际化处理,便于多语言支持。

📄 原文内容

Renders a “fake” meta box with an information message, shown on the block editor, when an incompatible meta box is found.

Parameters

$data_objectmixedrequired
The data object being rendered on this screen.
$boxarrayrequired
Custom formats meta box arguments.

  • id string
    Meta box 'id' attribute.
  • title string
    Meta box title.
  • old_callback callable
    The original callback for this meta box.
  • args array
    Extra meta box arguments.

Source

function do_block_editor_incompatible_meta_box( $data_object, $box ) {
	$plugin  = _get_plugin_from_callback( $box['old_callback'] );
	$plugins = get_plugins();
	echo '<p>';
	if ( $plugin ) {
		/* translators: %s: The name of the plugin that generated this meta box. */
		printf( __( 'This meta box, from the %s plugin, is not compatible with the block editor.' ), "<strong>{$plugin['Name']}</strong>" );
	} else {
		_e( 'This meta box is not compatible with the block editor.' );
	}
	echo '</p>';

	if ( empty( $plugins['classic-editor/classic-editor.php'] ) ) {
		if ( current_user_can( 'install_plugins' ) ) {
			$install_url = wp_nonce_url(
				self_admin_url( 'plugin-install.php?tab=favorites&user;=wordpressdotorg&save;=0' ),
				'save_wporg_username_' . get_current_user_id()
			);

			echo '<p>';
			/* translators: %s: A link to install the Classic Editor plugin. */
			printf( __( 'Please install the <a href="%s">Classic Editor plugin</a> to use this meta box.' ), esc_url( $install_url ) );
			echo '</p>';
		}
	} elseif ( is_plugin_inactive( 'classic-editor/classic-editor.php' ) ) {
		if ( current_user_can( 'activate_plugins' ) ) {
			$activate_url = wp_nonce_url(
				self_admin_url( 'plugins.php?action=activate&plugin;=classic-editor/classic-editor.php' ),
				'activate-plugin_classic-editor/classic-editor.php'
			);

			echo '<p>';
			/* translators: %s: A link to activate the Classic Editor plugin. */
			printf( __( 'Please activate the <a href="%s">Classic Editor plugin</a> to use this meta box.' ), esc_url( $activate_url ) );
			echo '</p>';
		}
	} elseif ( $data_object instanceof WP_Post ) {
		$edit_url = add_query_arg(
			array(
				'classic-editor'         => '',
				'classic-editor__forget' => '',
			),
			get_edit_post_link( $data_object )
		);
		echo '<p>';
		/* translators: %s: A link to use the Classic Editor plugin. */
		printf( __( 'Please open the <a href="%s">classic editor</a> to use this meta box.' ), esc_url( $edit_url ) );
		echo '</p>';
	}
}

Changelog

Version Description
5.0.0 Introduced.