luni, 9 august 2010
[Tips & Hacks] Custom Recent Comments Widget
Salut şi bine te-am găsit!
Prin acest post mă bucur să te pot anunţa o nouă serie de articole pe care începem să le publicăm pe blog – o serie despre optimizări şi hack-uri ce se pot aplica unui blog WordPress. Marea lor majoritate vor fi chiar implementate de noi pe blogul Blogway, aşa cum este cazul şi optimizării din acest post.
Astfel, Vica ne prezintă astăzi modificările aplicate widget-ului Recent Comments.
În mod obişnuit, “Recent Comments Widget” implementat în WordPress, afişează în sidebar comentariile recente (şi cele obişnuite şi cele de tip ‘pings‘).
În acest caz, am dorit un widget propriu, în care pe langă opţiunile implicite (titlu şi număr de comentarii afişate) să avem opţiunea de a afişa comentariile obişnuite, cele de tip pings sau toate comentariile.
Soluţie: Extinderea clasei Widget şi a funcţiilor acestora, cu modificările ce vor implementa metoda de selecţie (http://codex.wordpress.org/Widgets_API ):
class My_Widget extends WP_Widget {
function My_Widget() {
// widget actual processes
}
function form($instance) {
// outputs the options form on admin
}
function update($new_instance, $old_instance) {
// processes widget options to be saved
}
function widget($args, $instance) {
// outputs the content of the widget
}
}
register_widget('My_Widget');
Cea mai simplă metodă a fost “rescrierea” implementării widgetului original “Recent Comments” din \wp-include\default-widgets.php în functions.php (ce se află în directorul rădăcină al temei), astfel încât se va păstra şi varianta originală în lista de widgeturi implicite.
Modificările aduse implementarii originale sunt:
- extinderea vectorului $instance si instanţierea acestuia cu noile opţiuni, în metoda update():
if ( in_array( $new_instance['comment_type'], array( 'comments', 'pings', 'all' ) ) ) { $instance['comment_type'] = $new_instance['comment_type']; } else { $instance['comment_type'] = 'comments'; } - modificarea metodei ce afişează formularul widgetului în interfaţa de administrare, în metoda form():
$comment_type = isset($instance['comment_type']) ? esc_attr($instance['comment_type']) : 'comments'; ... <p> <label for="<?php echo $this->get_field_id('comment_type'); ?>"> Select type of comments to show:</label> <select id="<?php echo $this->get_field_id('comment_type'); ?>" name="<?php echo $this->get_field_name('comment_type'); ?>"> <option value="comments">Simple comments</option> <option value="pings">Pings</option> <option value="all">All comments</option> </select> </p> - modificarea metodei ce afişează outputul widgetului în sidebar-ul blogului, în metoda widget():
$comment_type = empty( $instance['comment_type'] ) ? 'comments' : $instance['comment_type']; switch( $comment_type ) { case "comments": $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' AND comment_type != 'trackback' AND comment_type != 'pingback' ORDER BY comment_date_gmt DESC LIMIT 15"); break; case "pings": $comments = $wpdb->get_results(" ... WHERE comment_approved = '1' AND post_status = 'publish' AND comment_type = 'trackback' OR comment_type = 'pingback' ..."); break; case "all": $comments = $wpdb->get_results(" ... WHERE comment_approved = '1' AND post_status = 'publish' ..."); break; }
Etichete: API, codex, comments widget, geek, recent comments, recent comments widget, sidebar, wordpress code




Super folositor.
Multumim Alexandru.