Adding a Custom Admin Dashboard Widget
Here's how to add a custom widget to your WordPress admin dashboard using the WordPress Dashboard Widgets API.
Add this code to your theme's functions.php
file or in a custom plugin:
Basic dashboard widget that displays static content:
function add_custom_dashboard_widget() {
wp_add_dashboard_widget(
'custom_dashboard_widget', // Widget slug
'My Custom Dashboard Widget', // Widget title
'custom_dashboard_widget_content' // Display function
);
}
add_action('wp_dashboard_setup', 'add_custom_dashboard_widget');
function custom_dashboard_widget_content() {
echo '<p>Welcome to your dashboard! Here\'s your important information.</p>';
}
Advanced widget with dynamic content and settings:
function add_custom_dashboard_widget_advanced() {
wp_add_dashboard_widget(
'custom_dashboard_widget_advanced',
'Site Statistics',
'display_dashboard_widget_content',
'configure_dashboard_widget_settings'
);
}
add_action('wp_dashboard_setup', 'add_custom_dashboard_widget_advanced');
function display_dashboard_widget_content() {
// Get widget options
$widget_options = get_option('custom_dashboard_widget_options');
$days = isset($widget_options['days']) ? $widget_options['days'] : 7;
// Get post count for specified days
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'date_query' => array(
array(
'after' => $days . ' days ago'
)
)
);
$recent_posts = new WP_Query($args);
echo '<p>Posts published in last ' . $days . ' days: ' . $recent_posts->found_posts . '</p>';
echo '<p>Total published posts: ' . wp_count_posts()->publish . '</p>';
}
function configure_dashboard_widget_settings() {
// Save settings
if (isset($_POST['submit'])) {
$widget_options = array(
'days' => absint($_POST['dashboard_widget_days'])
);
update_option('custom_dashboard_widget_options', $widget_options);
}
// Get current settings
$widget_options = get_option('custom_dashboard_widget_options');
$days = isset($widget_options['days']) ? $widget_options['days'] : 7;
// Display settings form
echo '<p>';
echo '<label for="dashboard_widget_days">Days to show: </label>';
echo '<input type="number" id="dashboard_widget_days" name="dashboard_widget_days" value="' . $days . '">';
echo '</p>';
}
Real-world example showing system information:
function add_system_info_dashboard_widget() {
wp_add_dashboard_widget(
'system_info_dashboard_widget',
'System Information',
'display_system_info_content'
);
}
add_action('wp_dashboard_setup', 'add_system_info_dashboard_widget');
function display_system_info_content() {
global $wpdb;
echo '<ul>';
echo '<li>WordPress Version: ' . get_bloginfo('version') . '</li>';
echo '<li>PHP Version: ' . phpversion() . '</li>';
echo '<li>MySQL Version: ' . $wpdb->db_version() . '</li>';
echo '<li>Active Theme: ' . wp_get_theme()->get('Name') . '</li>';
echo '<li>Active Plugins: ' . count(get_option('active_plugins')) . '</li>';
echo '</ul>';
}
Plugin Alternative
If you prefer using a plugin, here are some recommended options:
These plugins provide user-friendly interfaces for adding and managing dashboard widgets without coding.
Remember to adjust the widget content and settings according to your specific needs. The code examples above can be customized to display any type of information you want to show in your dashboard.