Creating Custom Dashboard Widgets in WordPress
Basic Approach
Dashboard widgets help you display important information directly on the WordPress admin dashboard. You can create them using WordPress's built-in functions.
Here's how to add a basic dashboard widget:
function my_custom_dashboard_widget() {
// Widget content goes here
echo '<p>Welcome to your custom dashboard widget!</p>';
}
function register_my_dashboard_widget() {
wp_add_dashboard_widget(
'my_dashboard_widget', // Widget slug
'My Custom Widget', // Title
'my_custom_dashboard_widget' // Display function
);
}
add_action('wp_dashboard_setup', 'register_my_dashboard_widget');
Advanced Widget with Configuration
Here's how to create a widget with configurable options:
function dashboard_widget_display() {
$visits = get_option('site_visits', 0);
echo '<div class="dashboard-widget-content">';
echo '<h4>Site Statistics</h4>';
echo '<p>Total Visits: ' . esc_html($visits) . '</p>';
echo '</div>';
}
function dashboard_widget_configure() {
if (isset($_POST['submit'])) {
update_option('site_visits', absint($_POST['visits']));
}
$visits = get_option('site_visits', 0);
?>
<form method="post">
<label for="visits">Total Visits:</label>
<input type="number" id="visits" name="visits" value="<?php echo esc_attr($visits); ?>">
<input type="submit" name="submit" value="Save" class="button button-primary">
</form>
<?php
}
function register_configured_dashboard_widget() {
wp_add_dashboard_widget(
'configured_dashboard_widget',
'Site Statistics',
'dashboard_widget_display',
'dashboard_widget_configure'
);
}
add_action('wp_dashboard_setup', 'register_configured_dashboard_widget');
Adding Styling
Add custom styles to your widget:
function add_dashboard_widget_styles() {
echo '
<style>
#configured_dashboard_widget .dashboard-widget-content {
padding: 12px;
}
#configured_dashboard_widget h4 {
margin-top: 0;
color: #23282d;
}
</style>
';
}
add_action('admin_head', 'add_dashboard_widget_styles');
Best Practices
- Use meaningful widget slugs
- Sanitize and escape data
- Follow WordPress coding standards
- Keep performance in mind
- Use nonces for form submissions
- Implement proper error handling
Security Considerations
- Always validate and sanitize input data
- Use WordPress security functions (esc_html, esc_attr)
- Implement capability checks
- Use nonces for forms
- Sanitize URLs and external data
Common Pitfalls
- Forgetting to escape output
- Not checking user capabilities
- Loading too much data
- Poor error handling
- Not following WordPress hooks order
Helpful Plugins
If you need ready-made solutions:
-
Stream - Tracks user activity and creates useful dashboard widgets
-
Google Analytics Dashboard - Adds analytics widgets
Example with Real Data
Here's a widget that displays recent posts and user count:
function site_overview_widget() {
$posts = wp_count_posts();
$users = count_users();
echo '<div class="site-overview">';
echo '<p>Published Posts: ' . esc_html($posts->publish) . '</p>';
echo '<p>Total Users: ' . esc_html($users['total_users']) . '</p>';
// Recent Posts
$recent_posts = get_posts(array(
'numberposts' => 5,
'post_status' => 'publish'
));
if ($recent_posts) {
echo '<h4>Recent Posts</h4>';
echo '<ul>';
foreach ($recent_posts as $post) {
printf(
'<li><a href="%s">%s</a></li>',
esc_url(get_permalink($post->ID)),
esc_html($post->post_title)
);
}
echo '</ul>';
}
echo '</div>';
}
function register_site_overview_widget() {
if (current_user_can('edit_posts')) {
wp_add_dashboard_widget(
'site_overview_widget',
'Site Overview',
'site_overview_widget'
);
}
}
add_action('wp_dashboard_setup', 'register_site_overview_widget');