wp_dashboard()
云策文档标注
概述
wp_dashboard() 函数用于显示 WordPress 管理后台的仪表盘,它通过获取当前屏幕对象和列数来布局 meta boxes。文档还包含一个用户贡献的示例,展示如何为自定义插件创建可重排小部件的仪表盘。
关键要点
- wp_dashboard() 是 WordPress 核心函数,用于渲染仪表盘界面,基于 get_current_screen() 获取屏幕信息。
- 函数内部使用 do_meta_boxes() 来输出不同区域(如 normal、side)的 meta boxes,支持多列布局。
- 用户贡献示例提供了创建自定义仪表盘的完整代码,包括菜单添加、页面导航、脚本加载和小部件注册。
- 相关函数包括 get_current_screen()、do_meta_boxes()、wp_nonce_field() 和 absint(),用于辅助功能实现。
代码示例
function wpdocs_plugin_admin_menu() {
add_submenu_page( 'tools.php', __( 'My Custom Plugin' ), __( 'My Custom Plugin' ), 'manage_options', 'wpdocs-plugin-slug', 'wpdocs_pages' );
}
add_action( 'admin_menu', 'wpdocs_plugin_admin_menu', 9999 );
function wpdocs_plugin_register_widgets() {
wp_add_dashboard_widget( 'wpdocs_plugin_widget_1', __( 'Custom Widget 1' ), 'wpdocs_plugin_widget_1', null, null, 'normal' );
wp_add_dashboard_widget( 'wpdocs_plugin_widget_2', __( 'Custom Widget 2' ), 'wpdocs_plugin_widget_2', null, null, 'side' );
wp_add_dashboard_widget( 'wpdocs_plugin_widget_3', __( 'Custom Widget 3' ), 'wpdocs_plugin_widget_3', null, null, 'side' );
}注意事项
- 自定义仪表盘示例需要确保用户权限(如 manage_options)和正确加载脚本(如 wp_enqueue_script('dashboard'))。
- 用户反馈指出示例代码可能存在滚动问题,建议检查 wpdocs_plugin_scripts() 函数以确保脚本正确加载。
原文内容
Displays the dashboard.
Source
function wp_dashboard() {
$screen = get_current_screen();
$columns = absint( $screen->get_columns() );
$columns_css = '';
if ( $columns ) {
$columns_css = " columns-$columns";
}
?>
<div id="dashboard-widgets" class="metabox-holder<?php echo $columns_css; ?>">
<div id="postbox-container-1" class="postbox-container">
id, 'normal', '' ); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
id, 'side', '' ); ?>
</div>
<div id="postbox-container-3" class="postbox-container">
id, 'column3', '' ); ?>
</div>
<div id="postbox-container-4" class="postbox-container">
id, 'column4', '' ); ?>
</div>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/dashboard.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/dashboard.php#L260">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/dashboard.php#L260-L287">View on GitHub</a></p></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_current_screen/">get_current_screen()</a><code>wp-admin/includes/screen.phpGet the current screen object
do_meta_boxes()wp-admin/includes/template.php Meta-Box template function.
wp_nonce_field()wp-includes/functions.php Retrieves or display nonce hidden field for forms.
absint()wp-includes/load.php Converts a value to non-negative integer.
Show 1 moreShow lessChangelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Need to make a custom dashboard for your custom plugin?
The following code will create a fully functional dashboard that can be rearranged by the user. Once the user rearranges it, the order of the widgets for that dashboard are saved in the user’s settings.