"SELECT ID AS id, post_title, comment_count FROM `" . $wpdb->posts . "` WHERE post_type = 'post' AND post_status = 'publish' AND comment_count > 0 ORDER BY comment_count DESC LIMIT 5", // When do your posts generate the most comments? 'comments_per_day' => "SELECT COUNT(comment_ID) AS count, DATE_FORMAT(comment_date, '%w') AS day FROM `" . $wpdb->comments . "` WHERE comment_approved = 1 AND DATE_FORMAT(comment_date, '%w') IS NOT NULL GROUP BY DATE_FORMAT(comment_date, '%w') ORDER BY day ASC", // Total comments approved 'comments_total' => "SELECT COUNT(comment_ID) AS count FROM `" . $wpdb->comments . "` WHERE comment_approved = '1'", // Total words per comments approved 'comments_word' => "SELECT SUM(LENGTH(comment_content) - LENGTH(REPLACE(comment_content, ' ', ''))+1) AS count FROM `" . $wpdb->comments . "` WHERE comment_approved = '1'", // 5 authors who comment the most 'comments_per_author' => "SELECT u.ID, u.user_login, u.display_name, COUNT(c.comment_ID) AS comment_count FROM `" . $wpdb->comments . "` c LEFT JOIN `" . $wpdb->users . "` u ON c.user_id = u.ID WHERE c.user_id != 0 GROUP BY c.user_id ORDER BY comment_count DESC LIMIT 5", // Total posts published 'posts_total' => "SELECT COUNT(ID) FROM `" . $wpdb->posts . "` WHERE post_type = 'post' AND post_status = 'publish'", // Total words per posts approved 'posts_word' => "SELECT SUM(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1) AS count FROM `" . $wpdb->posts . "` WHERE post_status = 'publish'", // Date of first post 'first_post' => "SELECT post_date FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date ASC LIMIT 1", // When do you post ? 'posts_per_day' => "SELECT COUNT(ID) AS count, DATE_FORMAT(post_date, '%w') AS day FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post' GROUP BY DATE_FORMAT(post_date, '%w') ORDER BY day ASC", // 5 posts which generate the most Facebook shares and likes 'posts' => "SELECT ID, post_title FROM `" . $wpdb->posts . "` WHERE post_status = 'publish' AND post_type = 'post'" ); // Starting date if (!empty($options['starting_date'])) { $startingdate = $options['starting_date'] . ' 00:00:00'; } else { $startingdate = (string)current( $wpdb->get_col( $queries['first_post'], 0 ) ); } // Elapsed days function date_diff_days($date1, $date2) { $s = strtotime( $date2 ) - strtotime( $date1 ); $d = intval( $s / 86400 ) + 1; return $d; } $elapseddays = date_diff_days( $startingdate, date( 'Y-m-d H:i:s' ) ); /********** ACTIONS **********/ add_action( 'admin_init', 'abm_admin_init' ); add_action( 'admin_menu', 'abm_admin_menu' ); add_filter( 'plugin_action_links_'. plugin_basename( __FILE__ ), 'abm_plugin_action_links', 10, 2 ); function abm_admin_init() { wp_register_style( 'advanced-blog-metrics', plugins_url( 'style.css', __FILE__ ) ); wp_enqueue_style( 'advanced-blog-metrics' ); register_setting( 'abm_options', 'abm_options', 'abm_options_validate' ); add_settings_section( 'abm_options_general', 'Settings', 'abm_options_general_text', 'abm_options' ); add_settings_field( 'abm_options_starting_date', '', 'abm_options_starting_date_text', 'abm_options', 'abm_options_general' ); // access only for administrator if (current_user_can('administrator') ) { add_action( 'wp_dashboard_setup', 'abm_dashboard_init' ); } } function abm_admin_menu() { add_menu_page( 'Advanced Blog Metrics', 'Advanced
Blog Metrics', 'administrator', 'advanced-blog-metrics', 'abm_options_page' ); } /********** SETTINGS **********/ function abm_options_page() { ob_start(); echo '
'; echo '

Advanced Blog Metrics

'; echo '
'; settings_errors(); settings_fields( 'abm_options' ); do_settings_sections( 'abm_options' ); submit_button(); echo '
'; echo '
'; ob_end_flush(); } function abm_options_general_text() { } function abm_options_starting_date_text() { $options = get_option( 'abm_options' ); echo 'Date format: YYYY-MM-DD'; echo '

If you leave this field empty, Advanced Blog Metrics uses the date of your first post by default.

'; } function abm_options_validate($posted) { $options = get_option( 'abm_options' ); $cleaned = array(); if ( !empty( $posted['starting_date'] ) && !preg_match( '`[0-9]{4}\-[0-9]{2}\-[0-9]{2}`', $posted['starting_date'] ) ) { add_settings_error('abm_options_starting_date', 'abm_options_starting_date_bad_format', 'You did not use the expected date format. Please, fill in the starting date with the following format: YYYY-MM-DD'); } elseif ( !empty( $posted['starting_date'] ) ) { list( $year, $month, $day ) = explode( '-', $posted['starting_date'] ); if ( !checkdate( $month, $day, $year ) ) { add_settings_error('abm_options_starting_date', 'abm_options_starting_date_not_valid', 'You have entered a date which does not exist. Please, fill in the starting date with a valid date.'); } } if ( count( get_settings_errors() ) > 0 && array_key_exists( 'starting_date', $options ) ) { $cleaned['starting_date'] = $options['starting_date']; } else { $cleaned['starting_date'] = $posted['starting_date']; } return $cleaned; } /********** INIT **********/ function abm_dashboard_init() { // Widget #1 wp_add_dashboard_widget( 'dashboard_comments_per_post', '5 posts which generate the most comments', 'dashboard_comments_per_post' ); // Widget #2 wp_add_dashboard_widget( 'dashboard_comments_per_day', 'When do your posts generate the most comments?', 'dashboard_comments_per_day' ); // Widget #3 wp_add_dashboard_widget( 'dashboard_comments', 'Comments', 'dashboard_comments' ); // Widget #4 wp_add_dashboard_widget( 'dashboard_comments_per_author', '5 authors who comment the most', 'dashboard_comments_per_author' ); // Widget #5 wp_add_dashboard_widget( 'dashboard_posts', 'Posts', 'dashboard_posts' ); // Widget #6 wp_add_dashboard_widget( 'dashboard_posts_per_day', 'When do you post the most?', 'dashboard_posts_per_day' ); // Widget #7 wp_add_dashboard_widget( 'dashboard_posts_facebook', '5 posts which generate the most Facebook shares and likes', 'dashboard_posts_facebook' ); } function abm_plugin_action_links( $links, $file ) { array_unshift( $links, '' . __( 'Settings' ) . '' ); return $links; } /***** WIDGETS ON DASHBOARD *****/ // widget #1 : Posts which generate the most comments function dashboard_comments_per_post() { global $queries, $wpdb; $posts = $wpdb->get_results( $queries['comments_per_post'] ); $html = ''; $html.= ''; $html.= ''; foreach ( $posts as $post ) { $html.= ''; $html.= ''; $html.= ''; $html.= ''; } $html.= ''; $html.= '
PostComments
' . $post->post_title . '' . $post->comment_count . '
'; echo $html; } // Widget #2 : When do your posts generate the most comments? function dashboard_comments_per_day() { global $queries, $wpdb, $days, $startofweek; $posts = array(); $results = $wpdb->get_results( $queries['comments_per_day'] ); foreach($results as $result) { $posts[$result->day] = $result->count; } $max = max($posts); $html.= ''; $html.= ''; for ($num_day = $startofweek; $num_day <= 6; $num_day++) { $html.= ''; } for ($num_day = 0; $num_day < $startofweek; $num_day++) { $html.= ''; } $html.= ''; for ($num_day = $startofweek; $num_day <= 6; $num_day++) { $html.= ''; } for ($num_day = 0; $num_day < $startofweek; $num_day++) { $html.= ''; } $html.= ''; $html.= '
'; $html.= '' . $posts[$num_day] . ''; $html.= '
'; $html.= '
'; $html.= '' . $posts[$num_day] . ''; $html.= '
'; $html.= '
' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '
'; echo $html; } // Widget #3 : Comments function dashboard_comments() { global $queries, $wpdb, $elapseddays; $total = current( $wpdb->get_col( $queries['comments_total'], 0 ) ); $posts = current( $wpdb->get_col( $queries['posts_total'], 0 ) ); $words = current( $wpdb->get_col( $queries['comments_word'], 0 ) ); $html.= ''; $html.= ''; $html.= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= '
Approved commentsComments per DayComments per PostWords per Comment
' . $total . '' . round( $total / $elapseddays, 2 ) . '' . round( $total / $posts, 1 ) . '' . round( $words / $total ) . '
'; echo $html; } // Widget #4 : Authors who comment the most function dashboard_comments_per_author() { global $queries, $wpdb, $commentregistration; if ($commentregistration) { $authors = $wpdb->get_results($queries['comments_per_author']); } $html = ''; $html.= ''; if ($commentregistration) { foreach ($authors as $author) { $html.= ''; $html.= ''; $html.= ''; $html.= ''; } } else { $html.= ''; } $html.= '
AuthorComments
' . $author->display_name . '' . $author->comment_count . '

Note that you need to check "Users must be registered and logged in to comment" in the Wordpress Settings->Discussion to see data in the "5 authors who comments the most" widget.

View discussion options

'; echo $html; } // Widget #5 : Posts function dashboard_posts() { global $queries, $wpdb, $elapseddays; $total = current( $wpdb->get_col( $queries['posts_total'], 0 ) ); $comments = current( $wpdb->get_col( $queries['comments_total'], 0 ) ); $words = current( $wpdb->get_col( $queries['posts_word'], 0 ) ); $html.= ''; $html.= ''; $html .= ''; $html .= ''; $html .= ''; $html .= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= ''; $html.= '
PostsPosts per DayComments per PostWords per Post
' . $total . '' . round( $total / $elapseddays, 2 ) . '' . round( $comments / $total, 1 ) . '' . round( $words / $total ) . '
'; echo $html; } // Widget #6 : When do you post the most? function dashboard_posts_per_day() { global $queries, $wpdb, $days, $startofweek; $posts = array(); $results = $wpdb->get_results( $queries['posts_per_day'] ); foreach($results as $result) { $posts[$result->day] = $result->count; } $max = max($posts); $html.= ''; $html.= ''; for ($num_day = $startofweek; $num_day <= 6; $num_day++) { $html.= ''; } for ($num_day = 0; $num_day < $startofweek; $num_day++) { $html.= ''; } $html.= ''; for ($num_day = $startofweek; $num_day <= 6; $num_day++) { $html.= ''; } for ($num_day = 0; $num_day < $startofweek; $num_day++) { $html.= ''; } $html.= ''; $html.= '
'; $html.= '' . $posts[$num_day] . ''; $html.= '
'; $html.= '
'; $html.= '' . $posts[$num_day] . ''; $html.= '
'; $html.= '
' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '' . strtoupper( substr( $days[$num_day], 0, 3 ) ) . '
'; echo $html; } // Widget #7 : 5 posts which generate the most Facebook shares and likes function dashboard_posts_facebook() { global $queries, $wpdb; $results = $wpdb->get_results( $queries['posts'] ); $posts_like = array(); $posts_share = array(); foreach ($results as $result) { $fql_query_url = 'https://graph.facebook.com/' . '/fql?q=SELECT+url,share_count,like_count+FROM+link_stat+WHERE+url=\'' . get_permalink( $result->ID ) . '\''; $fql_query_result = file_get_contents($fql_query_url); $fql_query_obj = json_decode($fql_query_result, true); $posts_like[] = array('title' =>$result->post_title, 'permalink' => $fql_query_obj[data][0]['url'], 'like_count' => $fql_query_obj[data][0]['like_count']); $posts_share[] = array('title' =>$result->post_title, 'permalink' => $fql_query_obj[data][0]['url'], 'share_count' => $fql_query_obj[data][0]['share_count']); } foreach($posts_like as $k => $v) { $like[$k] = $v['like_count']; } array_multisort($like, SORT_DESC, $posts_like); foreach($posts_share as $k => $v) { $share[$k] = $v['share_count']; } array_multisort($share, SORT_DESC, $posts_share); // About LIKES $html = ''; $html.= ''; $i=0; foreach ($posts_like as $post) { $i++; if( $i<=5 ) { $html.= ''; $html.= ''; $html.= ''; $html.= ''; } else { break; } } $html.= '
Post
' . $post['title'] . '
'; echo $html, "
"; // About SHARES $html = ''; $html.= ''; $i=0; foreach ($posts_share as $post) { $i++; if( $i<=5 ) { $html.= ''; $html.= ''; $html.= ''; $html.= ''; } else break; } $html.= '
Post
' . $post['title'] . '
'; echo $html; }