In this tutorial I will show you how to display a widget in various position of WordPress dashboard page.
WordPress doesn’t provide any API to change position of widget. Therefore we need to modify WordPress core $wp_meta_boxes
variable to change position of widget.
Displaying Widget on Top
Here is sample code to display widget on top
{
wp_add_dashboard_widget( 'example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function' );
global $wp_meta_boxes;
$normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
$example_widget_backup = array( 'example_dashboard_widget' => $normal_dashboard['example_dashboard_widget'] );
unset( $normal_dashboard['example_dashboard_widget'] );
$sorted_dashboard = array_merge( $example_widget_backup, $normal_dashboard );
$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
}
function example_dashboard_widget_function(){}
add_action("wp_dashboard_setup", "example_add_dashboard_widgets");
Displaying Widget on Right Side
By default new widgets are displayed in left bottom but you can force a widget to be displayed on right bottom
{
wp_add_dashboard_widget( 'example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function' );
global $wp_meta_boxes;
$my_widget = $wp_meta_boxes['dashboard']['normal']['core']['example_dashboard_widget'];
unset($wp_meta_boxes['dashboard']['normal']['core']['example_dashboard_widget']);
$wp_meta_boxes['dashboard']['side']['core']['example_dashboard_widget'] = $my_widget;
}
function example_dashboard_widget_function(){echo "s";}
add_action("wp_dashboard_setup", "example_add_dashboard_widgets");
Similarly you can position the widget almost anywhere by altering $wp_meta_boxes
variable.