On this page
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' )
Adds a new dashboard widget.
Parameters
$widget_idstring Required-
Widget ID (used in the
'id'attribute for the widget). $widget_namestring Required-
Title of the widget.
$callbackcallable Required-
Function that fills the widget with the desired content.
The function should echo its output. $control_callbackcallable Optional-
Function that outputs controls for the widget.
Default:
null $callback_argsarray Optional-
Data that should be set as the $args property of the widget array (which is the second parameter passed to your callback).
Default:
null $contextstring Optional-
The context within the screen where the box should display.
Accepts'normal','side','column3', or'column4'. Default'normal'.Default:
'normal' $prioritystring Optional-
The priority within the context where the box should show.
Accepts'high','core','default', or'low'. Default'core'.Default:
'core'
Source
File: wp-admin/includes/dashboard.php. View all references
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 );
}
Related
Uses
| Uses | Description |
|---|---|
| get_current_screen() wp-admin/includes/screen.php | Get the current screen object |
| add_meta_box() wp-admin/includes/template.php | Adds a meta box to one or more screens. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| esc_url() wp-includes/formatting.php | Checks and cleans a URL. |
| add_query_arg() wp-includes/functions.php | Retrieves a modified URL query string. |
Used By
| Used By | Description |
|---|---|
| wp_dashboard_setup() wp-admin/includes/dashboard.php | Registers dashboard widgets. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_add_dashboard_widget