Last updated on September 13, 2023

Custom Dashboard Widget

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Add a custom Dashboard Widget in WP Admin.

Adding a custom dashboard widget to WordPress can be a great way to provide users with important information and quick access to important admin pages. Here’s an example code snippet that demonstrates how to add a custom dashboard widget:

function wpsnippets_dashboard_widget() {
    wp_add_dashboard_widget( 'wpsnippets_dashboard_widget', __( 'Custom Dashboard Widget', 'wpsnippets' ), 'wpsnippets_dashboard_widget_content' );
}

function wpsnippets_dashboard_widget_content() {
    echo '<p>This is my custom dashboard widget!</p>';
    echo '<p>You can add any content you want here.</p>';
}

add_action( 'wp_dashboard_setup', 'wpsnippets_dashboard_widget' );

In the code snippet above, we define a function called wpsnippets_dashboard_widget that uses the wp_add_dashboard_widget function to add a new dashboard widget. This function takes three parameters: the widget ID, the widget title, and the callback function that will output the widget content.

We then define a second function called wpsnippets_dashboard_widget_content that outputs the content for the widget. In this example, we simply output two paragraphs of text, but you can add any content you want here.

Finally, we hook the wpsnippets_dashboard_widget function to the wp_dashboard_setup action using the add_action function. This ensures that the function is executed when the dashboard is being set up, allowing us to add our custom widget.

Once you have added this code snippet, the custom dashboard widget will appear on the WordPress dashboard for all users. You can modify this code to add more widgets or to customize the output of the widget.

Last updated on September 13, 2023. Originally posted on May 3, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *