函数文档

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.php

Get 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 less

Changelog

VersionDescription
2.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    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.

    1. Creates a menu item located here: Admin > Tools > My Custom Plugin
    2. 2 pages: Dashboard and Another Page (placeholder)
    3. Includes top navigation to navigate between pages. (needs some styling)
    4. This is fully functional dashboard with 3 placeholder widgets.
    5. Compatible with PHP 7.4+
     Tools > My Custom Plugin
     */
    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 );
    
    /**
     * This handles which template loads based on the GET vars
     */
    function wpdocs_plugin_pages() {
    
        global $current_user_id;
    
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( 'You do not have permission to view this page.' );
        }
    
        $tab = $_GET['tab'] ?? '';
    
        echo '
    '; // Load the header nav wpdocs_plugin_header(); if ( in_array( $tab, array( '', 'dashboard' ) ) ) { // Load Dashboard wpdocs_plugin_dashboard(); } elseif ( 'another-page' === $tab ) { // Load Another Page wpdocs_plugin_another_page(); } else { echo '

    ' . __( 'Error: Couldn't find the page you are looking for.' ) . '

    '; } echo '
    '; } /** * Loads the scripts needed on the Dashboard page */ function wpdocs_plugin_scripts() { $page = $_GET['page'] ?? ''; $tab = $_GET['tab'] ?? ''; // This condition is assumes you will have more than 1 page for your plugin's admin area // The other pages of your admin would be relying on $_GET['tab'] if ( 'wpdocs-plugin-slug' === $page && in_array( $tab, array( '', 'dashboard', ] ) ) { wp_enqueue_script( 'dashboard' ); wp_admin_css( 'dashboard' ); add_thickbox(); } } add_action( 'admin_enqueue_scripts', 'wpdocs_plugin_scripts', 9 ); /** * Registers the dashboard widgets to be displayed */ 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' ); } /** * Creates the display code for the custom widget */ function wpdocs_plugin_widget_1() { ?>

    Custom widget 1 info here

    Custom widget 2 info here

    Custom widget 3 info here

    Another Page

    Custom content goes here.

    My Custom Dashboard