Skip to content

Instantly share code, notes, and snippets.

@vinhboy
Created May 11, 2010 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinhboy/ae68f29fd288e78e4272 to your computer and use it in GitHub Desktop.
Save vinhboy/ae68f29fd288e78e4272 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Latest Comments
Plugin URI: http://vinhboy.com
Description: Show the latest comments with dates
Author: vinhboy
Version: 1
Author URI: http://vinhboy.com
*/
add_action("widgets_init", array('Widget_latestComments', 'register'));
register_activation_hook( __FILE__, array('Widget_latestComments', 'activate'));
register_deactivation_hook( __FILE__, array('Widget_latestComments', 'deactivate'));
class Widget_latestComments {
function activate(){
$data = array('size'=>'10','title'=>'Latest Comments');
if (!get_option('widget_name')){
add_option('widget_latestComments' , $data);
} else {
update_option('widget_latestComments' , $data);
}
}
function deactivate(){
delete_option('widget_latestComments');
}
function control(){
$data = get_option('widget_latestComments');
echo "<p><label>Title</label><br /><input name='widget_latestComments_title' type='text' size='35' value='".$data['title']."' /></p>";
echo "<p><label>How many?</label><br /><input name='widget_latestComments_size' type='text' size='3' value='".$data['size']."' /></p>";
if (isset($_POST['widget_latestComments_size'])){
$data['title'] = attribute_escape($_POST['widget_latestComments_title']);
$data['size'] = attribute_escape($_POST['widget_latestComments_size']);
update_option('widget_latestComments', $data);
}
}
function widget($args){
global $wpdb, $comments, $comment;
$data = get_option('widget_latestComments');
echo $args['before_widget'];
echo $args['before_title'] .$data['title']. $args['after_title'];
if ( !$comments = wp_cache_get( 'widget_latestComments', 'widget' ) ) {
$comments = $wpdb->get_results("SELECT $wpdb->comments.* FROM $wpdb->comments JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_status = 'publish' ORDER BY comment_date_gmt DESC LIMIT ".intval($data['size']));
wp_cache_add( 'widget_latestComments', $comments, 'widget' );
}
$comments = array_slice( (array) $comments, 0 );
echo "<ul id=\"latestComments\">";
if ( $comments ) : foreach ( (array) $comments as $comment) :
echo '<li class="recentcomments"><a href="'. esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a><br />'. date('F d, Y',strtotime($comment->comment_date)) .' - '.$comment->comment_author.' said <i>"'.substr($comment->comment_content,0,100).'..."</i></li>';
endforeach; endif;
echo "</ul>";
echo $args['after_widget'];
}
function register(){
register_sidebar_widget('Latest Comments', array('Widget_latestComments', 'widget'));
register_widget_control('Latest Comments', array('Widget_latestComments', 'control'));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment