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

Adds a new dashboard widget.


Parameters Parameters

$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) (Optional) Function that outputs controls for the widget.

Default value: null

$callback_args

(array) (Optional) Data that should be set as the $args property of the widget array (which is the second parameter passed to your callback).

Default value: null


Top ↑

Source Source

File: wp-admin/includes/dashboard.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;
	} elseif ( 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, true ) ) {
		$location = 'side';
	}

	$high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' );

	$priority = 'core';
	if ( in_array( $widget_id, $high_priority_widgets, true ) ) {
		$priority = 'high';
	}

	add_meta_box( $widget_id, $widget_name, $callback, $screen, $location, $priority, $callback_args );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Adding dashboard widgets
    Here is a simple dashboard widget:

    /**
     * Add a new dashboard widget.
     */
    function wpdocs_add_dashboard_widgets() {
    	wp_add_dashboard_widget( 'dashboard_widget', 'Example Dashboard Widget', 'dashboard_widget_function' );
    }
    add_action( 'wp_dashboard_setup', 'wpdocs_add_dashboard_widgets' );
    
    /**
     * Output the contents of the dashboard widget
     */
    function dashboard_widget_function( $post, $callback_args ) {
    	esc_html_e( "Hello World, this is my first Dashboard Widget!", "textdomain" );
    }
    
    
  2. Skip to note 3 content
    Contributed by Codex

    Adding widgets onto the side
    The function doesn’t allow you to choose where you want your widget to go and will automatically add it to the “core” which is the left side. However you are able to get it on the right side very easily.

    You can use the add_meta_box() function instead of wp_add_dashboard_widget. Simply specify ‘dashboard’ in place of the $post_type. For example:

    add_meta_box( 'id', 'Dashboard Widget Title', 'dash_widget', 'dashboard', 'side', 'high' );
    
  3. Skip to note 4 content

    As noted in comment #714, wp_add_dashboard_widget() does not offer a great deal of flexibility to position a widget on the dashboard; however, it is possible to add a widget to a specific dashboard column relying on add_meta_box() and its $context parameter.

    The example below removes all core dashboard widgets and adds a custom one to the third dashboard column (tested ltr only). It looks odd, but it proves the point. 🙃

    <?php
    namespace Exampledashboard;
    
    add_action( 'wp_dashboard_setup', __NAMESPACE__ . 'wp_dashboard_setup' );
    
    /**
     * Remove core widhets and add widget to Dashboard in third column.
     * @see https://developer.wordpress.org/reference/functions/add_meta_box/
     */
    function wp_dashboard_setup() {
    
    	// Remove Welcome panel.
    	remove_action( 'welcome_panel', 'wp_welcome_panel' );
    
    	// Remove all Dashboard widgets.
    	global $wp_meta_boxes;
    	unset( $wp_meta_boxes['dashboard'] );
    
    	// Add custom dashbboard widget.
    	add_meta_box( 'dashboard_widget_example',
    		__( 'Example Widget', 'example-text-domain' ),
    		__NAMESPACE__ . 'render_example_widget',
    		'dashboard',
    		'column3',  // $context: 'advanced', 'normal', 'side', 'column3', 'column4'
    		'high',     // $priority: 'high', 'core', 'default', 'low'
    	);
    }
    
    /**
     * Render widget.
     */
    function render_example_widget() {
    ?>
    	<p>Do something.</p>
    <?php
    }

You must log in before being able to contribute a note or feedback.