WordPress at a glance

wp_add_dashboard_widget() WP 1.0

Adds a new dashboard widget.

  • Global. Array. $wp_dashboard_control_callbacks
Works based on: add_meta_box()

No Hooks.

Return

Nothing (null).

Usage

wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback, $callback_args );
$widget_id(string) (required)
Widget ID (used in the 'id' attribute for the widget).
$widget_name(string) (required)
Title of the widget.
$callback(callable) (required)
Function that fills the widget with the desired content. The function should echo its output.
$control_callback(callable)
Function that outputs controls for the widget.
Default: null
$callback_args(array)
Data that should be set as the $args property of the widget array (which is the second parameter passed to your callback).
Default: null

Code of wp_add_dashboard_widget: wp-admin/includes/dashboard.php VER 4.9.8

<?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 );
}

Related Functions

From tag: metabox (widget)

More from category: Admin Menu

No comments
    Hello, !     Log In . Register