add_thickbox()
云策文档标注
概述
add_thickbox() 函数用于在 WordPress 中加载默认的 ThickBox JavaScript 和 CSS 文件,以支持模态框功能。它通过 wp_enqueue_script 和 wp_enqueue_style 实现资源排队,并包含针对网络管理界面的特殊处理。
关键要点
- 函数自动加载 ThickBox 的脚本和样式,简化模态框功能的集成。
- 在 is_network_admin() 条件下,会添加 admin_head 动作以调整路径。
- 如需自定义设置,可通过类似 media-upload.js 的额外文件实现,并依赖 thickbox 数组确保正确加载顺序。
- 调用时必须在 wp_enqueue_scripts、admin_enqueue_scripts 或 login_enqueue_scripts 钩子中执行,否则在调试模式下会触发 PHP 警告。
代码示例
function fnctn_add_thickbox() {
add_thickbox();
}
add_action('wp_enqueue_scripts', 'fnctn_add_thickbox');注意事项
避免直接调用 add_thickbox() 而不使用钩子,这可能导致脚本和样式排队错误,引发调试警告。务必遵循 WordPress 的排队最佳实践。
原文内容
Enqueues the default ThickBox js and css.
Description
If any of the settings need to be changed, this can be done with another js file similar to media-upload.js. That file should require array(‘thickbox’) to ensure it is loaded after.
Source
function add_thickbox() {
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
if ( is_network_admin() ) {
add_action( 'admin_head', '_thickbox_path_admin_subfolder' );
}
}
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Skip to note 2 content
fnctn
If you use the following without a hook:
add_thickbox();You will see the following PHP warning if debug mode is on:
Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information.</p><p>Notice: wp_enqueue_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information.
To prevent this issue when calling the method, use one of the three hooks mentioned in the error as so:
function fnctn_add_thickbox () { add_thickbox(); } add_action ( 'wp_enqueue_scripts','fnctn_add_thickbox' );