WordPress is a highly customizable platform, and one of the ways to customize it is by adding widgets to the admin dashboard. The wp_add_dashboard_widget function is used to add a new widget to the WordPress dashboard.
This function takes three parameters: the widget ID, the widget title, and the callback function that generates the widget output. The ID should be a unique identifier for the widget, and the title is the text that will be displayed as the widget heading. The callback function should return the content that will be displayed inside the widget.
Once the widget is added, it will appear on the dashboard as a box that contains the content generated by the callback function.
Here’s an example usage code:
function my_dashboard_widget() {
echo "Hello, World!";
}
function add_my_dashboard_widget() {
wp_add_dashboard_widget(
'my_dashboard_widget',
'My Dashboard Widget',
'my_dashboard_widget'
);
}
add_action('wp_dashboard_setup', 'add_my_dashboard_widget');
In this example, we define a callback function called my_dashboard_widget
that simply echoes the text "Hello, World!". We then define another function called add_my_dashboard_widget
that uses the wp_add_dashboard_widget
function to add our new widget. Finally, we use the add_action
function to hook our add_my_dashboard_widget
function to the wp_dashboard_setup
action, which ensures that our widget is added to the dashboard.