函数文档

iframe_header()

💡 云策文档标注

概述

iframe_header() 是一个用于 Thickbox 的通用 iframe 头部函数,主要生成管理界面的 HTML 头部结构。它通过设置内容类型、调用相关钩子和类来确保 iframe 页面在 WordPress 后台正确显示。

关键要点

  • 函数用途:为 Thickbox 提供 iframe 页面的通用头部,常用于插件或主题安装信息对话框。
  • 参数说明:接受两个可选参数:$title(iframe 页面标题,默认为空)和 $deprecated(未使用,默认为 false)。
  • 核心功能:禁用管理栏、设置 admin body 类、输出 HTML 头部并触发多个 admin 钩子(如 admin_head、admin_enqueue_scripts)。
  • 相关函数:涉及 get_current_screen()、_wp_admin_html_begin()、show_admin_bar() 等,用于屏幕对象获取和 HTML 生成。
  • 钩子应用:包括 admin_body_class 过滤器、admin_enqueue_scripts 和 admin_head 等动作钩子,允许开发者自定义 iframe 页面。
  • 使用场景:被 install_theme_information() 和 install_plugin_information() 等函数调用,显示主题或插件信息对话框。
  • 版本历史:自 WordPress 2.7.0 版本引入。

代码示例

function iframe_header( $title = '', $deprecated = false ) {
    global $hook_suffix, $admin_body_class, $body_id, $wp_locale;

    show_admin_bar( false );

    $admin_body_class = preg_replace( '/[^a-z0-9_-]+/i', '-', $hook_suffix );

    $current_screen = get_current_screen();

    header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
    _wp_admin_html_begin();
    // 后续输出 HTML 头部代码
}

📄 原文内容

Generic Iframe header for use with Thickbox.

Parameters

$titlestringoptional
Title of the Iframe page. Default empty.
$deprecatedbooloptional
Not used.

Default:false

Source

function iframe_header( $title = '', $deprecated = false ) {
global $hook_suffix, $admin_body_class, $body_id, $wp_locale;

show_admin_bar( false );

$admin_body_class = preg_replace( '/[^a-z0-9_-]+/i', '-', $hook_suffix );

$current_screen = get_current_screen();

header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
_wp_admin_html_begin();
?>
<title> › — </title>

<script type="text/javascript">
addLoadEvent = function(func){if(typeof jQuery!=='undefined')jQuery(function(){func();});else if(typeof wpOnload!=='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
function tb_close(){var win=window.dialogArguments||opener||parent||top;win.tb_remove();}
var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>',
pagenow = '<?php echo esc_js( $current_screen->id ); ?>',
typenow = '<?php echo esc_js( $current_screen->post_type ); ?>',
adminpage = '<?php echo esc_js( $admin_body_class ); ?>',
thousandsSeparator = '<?php echo esc_js( $wp_locale->number_format['thousands_sep'] ); ?>',
decimalPoint = '<?php echo esc_js( $wp_locale->number_format['decimal_point'] ); ?>',
isRtl = <?php echo (int) is_rtl(); ?>;
</script>

</head>

<body <?php echo $admin_body_id; ?>class="wp-admin wp-core-ui no-js iframe ">
<script type="text/javascript">
(function(){
var c = document.body.className;
c = c.replace(/no-js/, 'js');
document.body.className = c;
})();
</script>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/template.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/template.php#L2125">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/template.php#L2125-L2198">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_body_class/"><span class="hook-func">apply_filters</span>( ‘admin_body_class’, <nobr><span class="arg-type">string</span> <span class="arg-name">$classes</span></nobr> )</a></dt><dd><p>Filters the CSS classes for the body tag in the admin.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/"><span class="hook-func">do_action</span>( ‘admin_enqueue_scripts’, <nobr><span class="arg-type">string</span> <span class="arg-name">$hook_suffix</span></nobr> )</a></dt><dd><p>Fires when enqueuing scripts for all admin pages.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_head/"><span class="hook-func">do_action</span>( ‘admin_head’ )</a></dt><dd><p>Fires in head section for all admin pages.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_head-hook_suffix/"><span class="hook-func">do_action</span>( “admin_head-{$hook_suffix}” )</a></dt><dd><p>Fires in head section for a specific admin page.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_print_scripts/"><span class="hook-func">do_action</span>( ‘admin_print_scripts’ )</a></dt><dd><p>Fires when scripts are printed for all admin pages.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_print_scripts-hook_suffix/"><span class="hook-func">do_action</span>( “admin_print_scripts-{$hook_suffix}” )</a></dt><dd><p>Fires when scripts are printed for a specific admin page based on $hook_suffix.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_print_styles/"><span class="hook-func">do_action</span>( ‘admin_print_styles’ )</a></dt><dd><p>Fires when styles are printed for all admin pages.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/admin_print_styles-hook_suffix/"><span class="hook-func">do_action</span>( “admin_print_styles-{$hook_suffix}” )</a></dt><dd><p>Fires when styles are printed for a specific admin page based on $hook_suffix.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_user_locale/">get_user_locale()</a><code>wp-includes/l10n.php

Retrieves the locale of a user.

get_current_screen()wp-admin/includes/screen.php

Get the current screen object

_wp_admin_html_begin()wp-admin/includes/template.php

Prints out the beginning of the admin HTML header.

esc_js()wp-includes/formatting.php

Escapes single quotes, ", , &, and fixes line endings.

sanitize_html_class()wp-includes/formatting.php

Sanitizes an HTML classname to ensure it only contains valid characters.

bloginfo()wp-includes/general-template.php

Displays information about the current site.

is_rtl()wp-includes/l10n.php

Determines whether the current locale is right-to-left (RTL).

wp_enqueue_style()wp-includes/functions.wp-styles.php

Enqueues a CSS stylesheet.

show_admin_bar()wp-includes/admin-bar.php

Sets the display status of the admin bar.

_e()wp-includes/l10n.php

Displays translated text.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

admin_url()wp-includes/link-template.php

Retrieves the URL to the admin area for the current site.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

apply_filters()wp-includes/plugin.php

Calls the callback functions that have been added to a filter hook.

get_option()wp-includes/option.php

Retrieves an option value based on an option name.

Show 10 moreShow less

Used by Description
install_theme_information()wp-admin/includes/theme-install.php

Displays theme information in dialog box form.

install_plugin_information()wp-admin/includes/plugin-install.php

Displays plugin information in dialog box form.

Changelog

Version Description
2.7.0 Introduced.