Adding Custom Dashboard Widgets in WordPress Admin

Posted on 16th June 2023

Introduction

In this article, we will be discussing how to add custom dashboard widgets in WordPress admin. Dashboard widgets are a great way to display useful information to WordPress administrators on the backend. For example, you might want to display a widget with the latest news from your blog, or a list of the most recent comments. There are many possibilities for what you can display in a dashboard widget.

Adding a custom dashboard widget is a fairly simple process. First, you will need to create a function that will output the content of your widget. Then, you will need to register your widget with WordPress. Finally, you will need to add your widget to a dashboard page. We will walk you through each of these steps in more detail below.

Creating the Widget Content

The first step in creating a custom dashboard widget is to create the content for your widget. This can be anything you want, but it is important to keep the content concise and relevant. For this example, we will create a widget that displays the latest news from our blog. The content for our widget will be fetched from an RSS feed.

The code for our widget will be as follows:

function my_dashboard_widget_function() {
  // Fetch the RSS feed
  $rss = fetch_feed( 'http://example.com/feed' );
  
  // If the RSS feed was fetched successfully...
  if ( ! is_wp_error( $rss ) ) {
    // Get the 10 most recent items from the RSS feed
    $maxitems = $rss->get_item_quantity( 10 );
    $rss_items = $rss->get_items( 0, $maxitems );
  }
  
  // If the RSS feed was fetched successfully and there are items to display...
  if ( $maxitems > 0 ) {
    // Start the widget output
    echo '<ul>';
    
    // Loop through each RSS item and display it
    foreach ( $rss_items as $item ) {
      echo '<li>';
      echo '<a href="' . $item->get_permalink() . '">';
      echo $item->get_title();
      echo '</a>';
      echo '</li>';
    }
    
    // End the widget output
    echo '</ul>';
  }
}

In the code above, we first fetch the RSS feed from our blog. Then, we loop through the items in the RSS feed and display them in a list. That’s all there is to it!

Registering the Widget

Now that we have the content for our widget, we need to register it with WordPress. Registering a widget simply means telling WordPress about the widget and giving it a name. We do this by adding the following code to our plugin:

function my_register_dashboard_widgets() {
  wp_add_dashboard_widget(
    'my_dashboard_widget',         // Widget slug
    'My Dashboard Widget',          // Title
    'my_dashboard_widget_function' // Display function
  );
}
add_action( 'wp_dashboard_setup', 'my_register_dashboard_widgets' );

In the code above, we first create a function called my_register_dashboard_widgets(). This function registers our widget with WordPress. We then hook this function to the wp_dashboard_setup action, which tells WordPress to run our function when the dashboard is being set up.

In our my_register_dashboard_widgets() function, we use the wp_add_dashboard_widget() function to register our widget. This function takes three arguments: the widget slug, the widget title, and the function that outputs the widget content. In our case, we are using the my_dashboard_widget_function() function we created earlier to output our widget content.

Adding the Widget to the Dashboard

The final step in adding a custom dashboard widget is to add the widget to a dashboard page. By default, WordPress comes with two dashboard pages: the “Home” page and the “Activity” page. You can add a widget to either of these pages, or you can create a new dashboard page and add your widget to that.

For this example, we will add our widget to the “Home” page. To do this, we simply need to add the following code to our plugin:

function my_dashboard_widgets() {
  global $wp_meta_boxes;
  
  wp_add_dashboard_widget(
    'my_dashboard_widget',         // Widget slug
    'My Dashboard Widget',          // Title
    'my_dashboard_widget_function' // Display function
  );
}
add_action( 'wp_dashboard_setup', 'my_dashboard_widgets' );

In the code above, we first create a function called my_dashboard_widgets(). This function adds our widget to the “Home” page. We then hook this function to the wp_dashboard_setup action, which tells WordPress to run our function when the dashboard is being set up.

In our my_dashboard_widgets() function, we use the wp_add_dashboard_widget() function to add our widget to the “Home” page. This function takes three arguments: the widget slug, the widget title, and the function that outputs the widget content. In our case, we are using the my_dashboard_widget_function() function we created earlier to output our widget content.

Conclusion

In this article, we have discussed how to add custom dashboard widgets in WordPress admin. Dashboard widgets are a great way to display useful information to WordPress administrators on the backend. Adding a custom dashboard widget is a fairly simple process. First, you will need to create a function that will output the content of your widget. Then, you will need to register your widget with WordPress. Finally, you will need to add your widget to a dashboard page.

In order to add a custom dashboard widget, you need to first create a function that will output the content of the widget. This function must have the following parameters:

$args – an array of arguments that are passed to the function
$instance – an array of settings for the widget

Once you have created the function, you need to register it with WordPress. To do this, you need to use the following code:

function my_custom_dashboard_widgets() {
wp_add_dashboard_widget(‘my_custom_dashboard_widget’, ‘My Custom Dashboard Widget’, ‘my_custom_dashboard_widget_function’);
}
add_action(‘wp_dashboard_setup’, ‘my_custom_dashboard_widgets’);

Replace ‘my_custom_dashboard_widget’ with a unique identifier for your widget, ‘My Custom Dashboard Widget’ with the title of your widget, and ‘my_custom_dashboard_widget_function’ with the name of the function that you created earlier.

Once you have registered the widget, it will appear in the list of available widgets on the WordPress admin dashboard.