函数文档

wp_enqueue_embed_styles()

💡 云策文档标注

概述

wp_enqueue_embed_styles() 函数用于在嵌入 iframe 的头部队列化 CSS 样式。它通过注册和内联样式表来管理嵌入模板的样式加载。

关键要点

  • 函数主要功能:在嵌入 iframe 头部队列化 CSS,确保样式正确加载。
  • 兼容性处理:检查是否有插件通过移除 'embed_head' 钩子上的 'print_embed_styles' 动作来禁用功能,如有则直接返回。
  • 样式注册与内联:使用 wp_register_style() 注册一个空样式表,然后通过 wp_add_inline_style() 从文件添加内联 CSS 内容。
  • 依赖函数:涉及 wp_scripts_get_suffix()、wp_register_style()、wp_add_inline_style()、wp_enqueue_style()、has_action() 和 remove_action() 等核心函数。
  • 版本信息:该函数自 WordPress 6.4.0 版本引入。

📄 原文内容

Enqueues the CSS in the embed iframe header.

Source

function wp_enqueue_embed_styles() {
	// Back-compat for plugins that disable functionality by unhooking this action.
	if ( ! has_action( 'embed_head', 'print_embed_styles' ) ) {
		return;
	}
	remove_action( 'embed_head', 'print_embed_styles' );

	$suffix = wp_scripts_get_suffix();
	$handle = 'wp-embed-template';
	wp_register_style( $handle, false );
	wp_add_inline_style( $handle, file_get_contents( ABSPATH . WPINC . "/css/wp-embed-template$suffix.css" ) );
	wp_enqueue_style( $handle );
}

Changelog

Version Description
6.4.0 Introduced.