添加新的仪表板小部件。

原型

wp_add_dashboard_widget( string $widget_id, string $widget_name, callable $callback, callable $control_callback = null, array $callback_args = null )

参数

$widget_id (string) (Required) 窗口小部件ID(用于窗口小部件的“id”属性)。

$widget_name (string) (Required) 小部件的标题。

$callback (callable) (Required) 使用所需内容填充窗口小部件的功能。该函数应该回显其输出。

$control_callback (callable) (Optional)  输出窗口小部件控件的函数。

$callback_args (array) (Optional)  应设置为窗口小部件数组的$ args属性的数据(这是传递给回调的第二个参数)。

源文件

路径:wp-admin/includes/dashboard.php

<?php
...
function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null, $callback_args = null ) {
	$screen = get_current_screen();
	global $wp_dashboard_control_callbacks;
	$private_callback_args = array( '__widget_basename' => $widget_name );
	if ( is_null( $callback_args ) ) {
		$callback_args = $private_callback_args;
	} else if ( is_array( $callback_args ) ) {
		$callback_args = array_merge( $callback_args, $private_callback_args );
	}
	if ( $control_callback && current_user_can( 'edit_dashboard' ) && is_callable( $control_callback ) ) {
		$wp_dashboard_control_callbacks[$widget_id] = $control_callback;
		if ( isset( $_GET['edit'] ) && $widget_id == $_GET['edit'] ) {
			list($url) = explode( '#', add_query_arg( 'edit', false ), 2 );
			$widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( $url ) . '">' . __( 'Cancel' ) . '</a></span>';
			$callback = '_wp_dashboard_control_callback';
		} else {
			list($url) = explode( '#', add_query_arg( 'edit', $widget_id ), 2 );
			$widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( "$url#$widget_id" ) . '" class="edit-box open-box">' . __( 'Configure' ) . '</a></span>';
		}
	}
	$side_widgets = array( 'dashboard_quick_press', 'dashboard_primary' );
	$location = 'normal';
	if ( in_array($widget_id, $side_widgets) )
		$location = 'side';
	$priority = 'core';
	if ( 'dashboard_browser_nag' === $widget_id )
		$priority = 'high';
	add_meta_box( $widget_id, $widget_name, $callback, $screen, $location, $priority, $callback_args );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_add_dashboard_widget/