Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active August 29, 2015 14:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpataki/dc082c2df80e075c86ff to your computer and use it in GitHub Desktop.
Save danielpataki/dc082c2df80e075c86ff to your computer and use it in GitHub Desktop.
Dahsboard Widgets
global $wpdb;
for( $i=1; $i <= 5; $i++ ) {
$this_week = 7 * $i;
$last_week = 7 * ( $i - 1);
$comment_counts[] =
$wpdb->get_var( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_date > '" . date('Y-m-d H:i:s', strtotime('-' . $this_week . ' days')) . "' AND comment_date < '" . date('Y-m-d H:i:s', strtotime('-' . $last_week . ' days')) . "'" ) ;
add_action( 'admin_enqueue_scripts', 'dashboard_widget_display_enqueues' );
function dashboard_widget_display_enqueues( $hook ) {
if( 'index.php' != $hook ) {
return;
}
wp_enqueue_style( 'dashboard-widget-styles', plugins_url( '', __FILE__ ) . '/widgets.css' );
}
function register_my_dashboard_widget() {
global $wp_meta_boxes;
wp_add_dashboard_widget(
'my_dashboard_widget',
'Publication Schedule',
'my_dashboard_widget_display'
);
$dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
$my_widget = array( 'my_dashboard_widget' => $dashboard['my_dashboard_widget'] );
unset( $dashboard['my_dashboard_widget'] );
$sorted_dashboard = array_merge( $my_widget, $dashboard );
$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
}
add_action( 'wp_dashboard_setup', 'register_my_dashboard_widget' );
function my_dashboard_widget_display() {
?>
<p>
Don't forget that there are three slots for publication each day. <strong>8am, 1pm and 6pm</strong> EDT. Posts must be finalized 12 hours before their publication time. This includes final checks and proofreading.
</p>
<p>
For particularly time-sensitive posts please contact the editor in chief to make sure the required time does not conflict with any ongoing projects.
</p>
<?php
}
function dashboard_widget_display_enqueues( $hook ) {
if( 'index.php' != $hook ) {
return;
}
wp_enqueue_style( 'dashboard-widget-styles', plugins_url( '', __FILE__ ) . '/widgets.css' );
}
add_action( 'admin_enqueue_scripts', 'dashboard_widget_display_enqueues' );
function register_comment_stats_dashboard_widget() {
wp_add_dashboard_widget(
'comment_stats_widget',
'Comment Stats',
'comment_stats_dashboard_widget_display'
);
}
add_action( 'wp_dashboard_setup', 'register_comment_stats_dashboard_widget' );
function comment_stats_dashboard_widget_display() {
$comment_counts = array( 20, 29, 39, 33, 17 );
$highest_value = max( $comment_counts );
$data_points = count( $comment_counts );
$bar_width = 100 / $data_points - 2;
$total_height = 120;
?>;
<div class="comment-stat-bars" style="height:<?php echo $total_height ?>px;">
<?php
foreach( $comment_counts as $count ) :
$count_percentage = $count/$highest_value;
$bar_height = $total_height * $count_percentage;
$border_width = $total_height - $bar_height;
?>
<div class="comment-stat-bar" style="height:<?php echo $total_height ?>px; border-top-width:<?php echo $border_width ?>px; width: <?php echo $bar_width ?>%;"></div>
<?php endforeach ?>
</div>
<div class='comment-stat-labels'>
<?php foreach( $comment_counts as $count ) : ?>
<div class='comment-stat-label' style='width: <?php echo $bar_width ?>%;'><?php echo $count ?></div>
<?php endforeach ?>
</div>
<div class='comment-stat-caption'>Comments in the past 5 weeks</div>
<?php
}
<div class="comment-stat-bars">
<div class="comment-stat-bar"></div>
<div class="comment-stat-bar"></div>
<!-- etc... -->
</div>
<div class="comment-stat-labels">
<div class="comment-stat-label">comment count here</div>
</div>
<div class='comment-stat-caption'>Comments in the past 5 weeks</div>
add_action( 'wp_dashboard_setup', 'register_my_dashboard_widget' );
function register_my_dashboard_widget() {
wp_add_dashboard_widget(
'my_dashboard_widget',
'My Dashboard Widget',
'my_dashboard_widget_display'
);
}
function my_dashboard_widget_display() {
echo 'Hello, I am Mr. Widget';
}
.comment-stat-bars {
overflow:hidden
}
.comment-stat-bar {
margin:0 1%;
background-color: #2ea2cc;
border-top-color: #fff;
border-top-style: solid;
float:left;
}
.comment-stat-labels {
overflow:hidden;
}
.comment-stat-label {
float:left;
margin:0 1%;
padding:5px 0;
text-align:center;
}
.comment-stat-caption {
text-align:center;
margin:11px 0;
font-style:italic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment