Skip to content

Instantly share code, notes, and snippets.

@tarex
Created August 26, 2012 13:51
Show Gist options
  • Save tarex/3479610 to your computer and use it in GitHub Desktop.
Save tarex/3479610 to your computer and use it in GitHub Desktop.
wordpress plugin [widget] - show recent comments with category
<?php
/*
Plugin Name: Extended Commnets
Plugin URI: https://gist.github.com/gists/3479610
Description: wordpress plugin [widget] - show recent comments with category
Version: 0.1
Author: Tareq Jobayere
Author URI: http://twitter.com/tareq_jobayere
License: i don't know
*/
class tarex_extended_Comments extends WP_Widget {
function __construct()
{
$params = array(
'description' => 'Display recent comments',
'name' => 'Extended Comments'
);
parent::__construct('Extended','',$params);
}
public function form($instance)
{
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input
type="text"
class="widefat"
id="<?php echo $this->get_field_id('title'); ?>"
name="<?php echo $this->get_field_name('title'); ?>"
value="<?php if( isset($title) ) echo esc_attr($title);?>"
/>
</p>
<p>
<label for="<?php echo $this->get_field_id('tcount'); ?>"><?php _e('Number of comments to show:'); ?></label>
<input
type="text"
id="<?php echo $this->get_field_id('tcount'); ?>"
name="<?php echo $this->get_field_name('tcount'); ?>"
value="<?php if( isset($title) ) echo esc_attr($tcount);?>"
size="3"
/>
</p>
<p>
<?php $link_cats = get_terms('category', 'orderby=count&hide_empty=0'); ?>
<label for="<?php echo $this->get_field_id('category'); ?>"><?php _e( 'Select Category:' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
<option value="0"><?php _ex('All', 'links widget'); ?></option>
<?php
foreach ( $link_cats as $link_cat ) {
echo '<option value="' . intval($link_cat->term_id) . '"'
. ( $link_cat->term_id == $instance['category'] ? ' selected="selected"' : '' )
. '>' . $link_cat->name . "</option>\n";
}
?>
</select>
</p>
<?php
} //end of form
// showing on the page
public function widget($args , $instance)
{
extract($args);
extract($instance);
$title = apply_filters('widget_title', $title);
// personal checking
if( empty($title) ){
$title = "Recent Comments";
}
if( empty($tcount) ){
$tcount = 10;
}
if( empty($category) ){
$category = 1;
}
$item = array(
'status' => 'approve',
'number' => $tcount,
'post_status' => 'publish'
);
$comments = get_comments($item);
$output = '';
$i=0;
$output.= $before_widget;
$output .= $before_title . $title . $after_title;
$output .= '<ul id="recentcomments">';
foreach ($comments as $comment)
{
$comm_post_id = $comment->comment_post_ID;
if (!in_category($category , $comm_post_id))
continue;
$i++;
$commentTime = date('M j, H:i a',strtotime($comment->comment_date) );
$CommentTxt = trim( mb_substr( strip_tags( apply_filters( 'comment_text', $comment->comment_content )), 0, 100 )); //100 is length
if( strlen($comment->comment_content) > 100){
$CommentTxt .= "...";
}
$output .= '<li class="recentcomments">';
$output .= '<a href="'.esc_url( get_comment_link($comment->comment_ID) ).'">'.get_comment_author_link($comment->comment_ID).'</a>';
$output .= ' on ';
$output .= '<a href="'.esc_url( get_comment_link($comment->comment_ID) ).'">'.get_the_title($comment->comment_post_ID).' </a>';
$output .= '<br><small>'.$commentTime.'</small>';
$output .= '<p>'.$CommentTxt.'</p>';
$output .='</li>';
if ($i >= $tcount) break;
}
$output .= '</ul>';
$output.= $after_widget;
echo $output;
}// end of widget
}// end of class
add_action('widgets_init', 'ex_Init');
function ex_Init() {
register_widget('tarex_extended_Comments');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment