handle_legacy_widget_preview_iframe

函数
handle_legacy_widget_preview_iframe ( No parameters )

handle_legacy_widget_preview_iframe: 这个函数用来处理WordPress中的传统小工具预览iframe: 该函数接收一个小工具实例,并返回一个预览iframe的HTML字符串。

拦截任何在查询参数中包含legacy-widget-preview的请求,如果设置了,则渲染一个包含请求的Legacy Widget块的预览的页面。

function handle_legacy_widget_preview_iframe() {
	if ( empty( $_GET['legacy-widget-preview'] ) ) {
		return;
	}

	if ( ! current_user_can( 'edit_theme_options' ) ) {
		return;
	}

	define( 'IFRAME_REQUEST', true );

	?>
	<!doctype html>
	<html <?php language_attributes(); ?>>
	<head>
		<meta charset="<?php bloginfo( 'charset' ); ?>" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<link rel="profile" href="https://gmpg.org/xfn/11" />
		<?php wp_head(); ?>
		<style>
			/* Reset theme styles */
			html, body, #page, #content {
				padding: 0 !important;
				margin: 0 !important;
			}

			/* Hide root level text nodes */
			body {
				font-size: 0 !important;
			}

			/* Hide non-widget elements */
			body *:not(#page):not(#content):not(.widget):not(.widget *) {
				display: none !important;
				font-size: 0 !important;
				height: 0 !important;
				left: -9999px !important;
				max-height: 0 !important;
				max-width: 0 !important;
				opacity: 0 !important;
				pointer-events: none !important;
				position: absolute !important;
				top: -9999px !important;
				transform: translate(-9999px, -9999px) !important;
				visibility: hidden !important;
				z-index: -999 !important;
			}

			/* Restore widget font-size */
			.widget {
				font-size: var(--global--font-size-base);
			}
		</style>
	</head>
	<body <?php body_class(); ?>>
		<div id="page" class="site">
			<div id="content" class="site-content">
				<?php
				$registry = WP_Block_Type_Registry::get_instance();
				$block    = $registry->get_registered( 'core/legacy-widget' );
				echo $block->render( $_GET['legacy-widget-preview'] );
				?>
			</div><!-- #content -->
		</div><!-- #page -->
		<?php wp_footer(); ?>
	</body>
	</html>
	<?php

	exit;
}

常见问题

FAQs
查看更多 >