wppaste
WordPress

wp_add_dashboard_widget( string $widget_id, string $widget_name, callable $callback, callable $control_callback = null, array $callback_args = null, string $context = 'normal', string $priority = 'core' )

Since
2.7.0, 5.6.0
Source
wp-admin/includes/dashboard.php:188
Adds a new dashboard widget.

Compatibility

WordPress
since 5.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$widget_idstring
Widget ID (used in the 'id' attribute for the widget).
$widget_namestring
Title of the widget.
$callbackcallable
Function that fills the widget with the desired content.
The function should echo its output.
$control_callbackcallableoptional
Function that outputs controls for the widget. Default null.Default: null
$callback_argsarrayoptional
Data that should be set as the $args property of the widget array (which is the second parameter passed to your callback). Default null.Default: null
$contextstringoptional
The context within the screen where the box should display.
Accepts 'normal', 'side', 'column3', or 'column4'. Default 'normal'.Default: 'normal'
$prioritystringoptional
The priority within the context where the box should show.
Accepts 'high', 'core', 'default', or 'low'. Default 'core'.Default: 'core'

Performance profile

How much work a call to wp_add_dashboard_widget() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
35–86

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 112.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_add_dashboard_widget()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_add_dashboard_widget() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always35–39get_current_screen(), add_meta_box()
!is_callable()39–43get_current_screen(), is_callable(), add_meta_box()
$callback_args !== null && is_array($callback_args)40–44get_current_screen(), array_merge(), add_meta_box()
is_callable() && !current_user_can()43–47get_current_screen(), is_callable(), current_user_can(), add_meta_box()
$callback_args !== null && is_array($callback_args) && !is_callable()44–48get_current_screen(), array_merge(), is_callable(), add_meta_box()
$callback_args !== null && is_array($callback_args) && is_callable() && !current_user_can()48–52get_current_screen(), array_merge(), is_callable(), current_user_can(), add_meta_box()
is_callable() && current_user_can()73–81get_current_screen(), is_callable(), current_user_can(), add_query_arg(), explode(), esc_url(), __(), add_meta_box()
$callback_args !== null && is_array($callback_args) && is_callable() && current_user_can()78–86get_current_screen(), array_merge(), is_callable(), current_user_can(), add_query_arg(), explode(), esc_url(), __(), add_meta_box()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 112 instructions, 35–86 executed per call, 11 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

Used by · 1

Source code

function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null, $callback_args = null, $context = 'normal', $priority = 'core' ) {	global $wp_dashboard_control_callbacks; 	$screen = get_current_screen(); 	$private_callback_args = array( '__widget_basename' => $widget_name ); 	if ( is_null( $callback_args ) ) {		$callback_args = $private_callback_args;	} elseif ( is_array( $callback_args ) ) {		$callback_args = array_merge( $callback_args, $private_callback_args );	} 	if ( $control_callback && is_callable( $control_callback ) && current_user_can( 'edit_dashboard' ) ) {		$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' ); 	if ( in_array( $widget_id, $side_widgets, true ) ) {		$context = 'side';	} 	$high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' ); 	if ( in_array( $widget_id, $high_priority_widgets, true ) ) {		$priority = 'high';	} 	if ( empty( $context ) ) {		$context = 'normal';	} 	if ( empty( $priority ) ) {		$priority = 'core';	} 	add_meta_box( $widget_id, $widget_name, $callback, $screen, $context, $priority, $callback_args );}

Changelog

Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.6.0
The $context and $priority parameters were added.from the docblock
2.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/dashboard.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.