Want an easy overview of your Groundhogg data? It’s super simple to create personalized dashboard widgets.
Here are a couple of snippets of code to add dashboard widgets using Groundhogg data. Add this code to your theme’s functions.php file or a code snippets plugin.
Removes all default WordPress dashboard widgets.
This first snippet will remove the WordPress default widgets (At a glance, Activity, Quick Draft, WordPress News, Welcome, Site Health Status).
/**
* Removes all default WordPress dashboard widgets.
*/
function remove_all_default_dashboard_widgets() {
global $wp_meta_boxes;
// Remove the 'At a Glance' widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
// Remove the 'Activity' widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
// Remove the 'Quick Draft' widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
// Remove the 'WordPress News' widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
// Remove the 'Welcome' panel
remove_action('welcome_panel', 'wp_welcome_panel');
// Remove the "Site Health Status" widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health']);
}
// Hook into 'wp_dashboard_setup' to remove the widgets
add_action('wp_dashboard_setup', 'remove_all_default_dashboard_widgets');
Display a total count of all contacts in Groundhogg
Displays a count of all Contacts with a link to the Contact List view.
/**
* Gets the total number of Contacts.
*
* @return int The total number of contacts from Contacts.
*/
function get_total_contacts() {
return \Groundhogg\get_db('contacts')->count();
}
function enqueue_dashboard_widget_styles() {
$custom_css = "
.contact-stats-widget-wrapper {
display: block; /* Make the wrapper a block-level element */
width: 100%; /* Full width of the widget */
height: 100%; /* Full height of the widget */
text-decoration: none; /* Remove underline */
}
.contact-stats-widget {
border: 2px solid #0073aa; /* Square border color */
border-radius: 5px; /* Rounded corners */
padding: 20px; /* Space inside the box */
background-color: #f1f1f1; /* Background color */
text-align: center; /* Center the text */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* Subtle shadow */
}
.contact-stats-widget h2 {
font-size: 24px; /* Larger font size for the title */
margin-bottom: 10px; /* Space below the title */
}
.contact-stats-widget p {
font-size: 20px; /* Larger font size for the content */
margin: 0; /* Remove default margin */
}
";
wp_add_inline_style('wp-admin', $custom_css);
}
add_action('admin_enqueue_scripts', 'enqueue_dashboard_widget_styles');
/**
* Displays the total number of contacts in a dashboard widget.
*/
function display_total_contacts_widget() {
$total_contacts = get_total_contacts();
echo '<a href="/wp-admin/admin.php?page=gh_contacts" class="contact-stats-widget-wrapper">';
echo '<div class="contact-stats-widget"><h2>Total Contacts:</h2> <p><strong>' . esc_html(number_format($total_contacts)) . '</strong></p></div>';
echo '</a>';
}
/**
* Adds a custom widget to the WordPress dashboard.
*/
function add_contact_total_widget_to_dashboard() {
wp_add_dashboard_widget(
'total_contacts_widget', // Widget slug
'Total Contacts', // Title
'display_total_contacts_widget' // Display function
);
}
// Hook into the 'wp_dashboard_setup' action to register the widget
add_action('wp_dashboard_setup', 'add_contact_total_widget_to_dashboard');
Gets the total number of emails sent from the email logs
These dashboard widgets will use Groundhogg data to show the number of emails sent today, this week, and this month, with a link to the email report.
/**
* Gets the total number of emails sent today from the email logs.
*
* @return int The total number of emails sent today.
*/
function get_total_emails_sent_today() {
return \Groundhogg\get_db('email_log')->count([
'where' => [
'relationship' => 'AND',
['date_sent', '>', \Groundhogg\Ymd_His(strtotime('today 12:00 AM'))],
['date_sent', '<', \Groundhogg\Ymd_His(strtotime('tomorrow 12:00 AM') - 1)]
]
]);
}
/**
* Gets the total number of emails sent this week from the email logs.
*
* @return int The total number of emails sent this week.
*/
function get_total_emails_sent_this_week() {
return \Groundhogg\get_db('email_log')->count([
'where' => [
'relationship' => 'AND',
['date_sent', '>', \Groundhogg\Ymd_His(strtotime('monday this week 12:00 AM'))],
['date_sent', '<', \Groundhogg\Ymd_His(strtotime('sunday this week 11:59 PM') + 86399)]
]
]);
}
/**
* Gets the total number of emails sent this month from the email logs.
*
* @return int The total number of emails sent this month.
*/
function get_total_emails_sent_this_month() {
return \Groundhogg\get_db('email_log')->count([
'where' => [
'relationship' => 'AND',
['date_sent', '>', \Groundhogg\Ymd_His(strtotime('first day of this month 12:00 AM'))],
['date_sent', '<', \Groundhogg\Ymd_His(strtotime('first day of next month 12:00 AM') - 1)]
]
]);
}
function enqueue_dashboard_widget_styles_2() {
$custom_css = "
.contact-stats-widget-wrapper {
display: block; /* Make the wrapper a block-level element */
width: 100%; /* Full width of the widget */
height: 100%; /* Full height of the widget */
text-decoration: none; /* Remove underline */
}
.contact-stats-widget {
border: 2px solid #0073aa; /* Square border color */
border-radius: 5px; /* Rounded corners */
padding: 20px; /* Space inside the box */
background-color: #f1f1f1; /* Background color */
text-align: center; /* Center the text */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* Subtle shadow */
}
.contact-stats-widget h2 {
font-size: 24px; /* Larger font size for the title */
margin-bottom: 10px; /* Space below the title */
}
.contact-stats-widget p {
font-size: 20px; /* Larger font size for the content */
margin: 0; /* Remove default margin */
}
";
wp_add_inline_style('wp-admin', $custom_css);
}
add_action('admin_enqueue_scripts', 'enqueue_dashboard_widget_styles_2');
/**
* Displays the total number of emails sent today, this week, and this month in a dashboard widget.
*/
function display_emails_sent_widget() {
$total_emails_today = get_total_emails_sent_today();
$total_emails_week = get_total_emails_sent_this_week();
$total_emails_month = get_total_emails_sent_this_month();
echo '<a href="/wp-admin/admin.php?page=gh_reporting&tab=email" class="contact-stats-widget-wrapper">';
echo '<div class="contact-stats-widget">';
echo '<h2>Email Stats:</h2>';
echo '<p>Today: <strong>' . esc_html($total_emails_today) . '</strong></p>';
echo '<p>This Week: <strong>' . esc_html($total_emails_week) . '</strong></p>';
echo '<p>This Month: <strong>' . esc_html($total_emails_month) . '</strong></p>';
echo '</div>';
echo '</a>';
}
/**
* Adds a custom widget to the WordPress dashboard.
*/
function add_emails_sent_widget_to_dashboard() {
wp_add_dashboard_widget(
'emails_sent_widget', // Widget slug
'Email Sent Stats', // Title
'display_emails_sent_widget' // Display function
);
}
// Hook into the 'wp_dashboard_setup' action to register the widget
add_action('wp_dashboard_setup', 'add_emails_sent_widget_to_dashboard');
Conclusion
These widgets provide a quick and convenient way to view statistics directly on your dashboard.
Get started with Groundhogg!
To get started with Groundhogg, you can:
- Start a 14-day demo.
- Purchase a plan.
- Take the free quickstart course.
- Keep reading!
Have questions? Don’t hesitate to shoot us a message!