WordPress : un widget « Recent comments » moins spammy ?…

De plus en plus nombreux sont les webmasters qui veillent à la qualité des liens pointant vers leur site web. En effet, Google n’apprécie plus vraiment l’apparition de liens massifs d’un site vers un autre, et en la matière, les plugins par défaut de WordPress peuvent provoquer ce genre de chose : le widget Recent Comments en fait parti !… La preuve en est la recrudescence des spammeurs webmasters qui viennent commenter sur mes différents blogs, et me demandent quelques jours plus tard de virer leur lien (!) parfois sur un ton on ne peut plus agressif !

Le widget Recent comments, mal configuré (par exemple, comme c’était le cas sur ce blog avant ce matin 😉 ), affichera les derniers commentaires validés sur le site sur… toutes les pages de ce dernier ! Sur un blog avec plusieurs centaines d’articles, des archives, des taxonomies à foison, etc. cela peut provoquer en une action, l’apparition de centaines voire de milliers de liens ! Une astuce consisterait à ne l’afficher que sur la home (par exemple) mais j’ai choisi une autre solution plus… radicale, et à mon avis meilleure puisque les liens resteront cantonnés à chaque article (et de ce fait seront mieux optimisés d’un point de vue sémantique – pour les spammeurs s’entend).

Voici donc une version « optimisée » de ce dernier, sous forme de plugin pour les plus fainéants (et donc sans nécessité de modifier le code de WordPress… puisque l’astuce ne réside qu’en l’ajout d’un strip_tags sur la fonction get_comment_author_link ! Je ne voulais pas virer les liens partout, articles compris, sinon un simple filtre aurait suffi pour cela)

<?php /*     Plugin Name: effiRecentComments     Plugin URI: https://www.effi10.com/recent-comments-plugin     Description: Extends the default Recent Comments plugin, by simply delete the author link     Author: Cedric GIRARD     Version: 1.0     Author URI: https://www.effi10.com     Copyright 2015  Cedric GIRARD  (email : contact@effi10.com)     This program is free software; you can redistribute it and/or modify     it under the terms of the GNU General Public License, version 2, as     published by the Free Software Foundation.     This program is distributed in the hope that it will be useful,     but WITHOUT ANY WARRANTY; without even the implied warranty of     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     GNU General Public License for more details.     You should have received a copy of the GNU General Public License     along with this program; if not, write to the Free Software     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */ class WP_effiRecentCommentsWidget extends WP_Widget { 	function __construct() { 		$widget_ops = array('classname' => 'effiwidget_recent_comments', 'description' => __( 'The most recent comments, but without author links' ) ); 		parent::__construct('recent-comments', __('effiRecent Comments'), $widget_ops); 		$this->alt_option_name = 'effiwidget_recent_comments'; 		if ( is_active_widget(false, false, $this->id_base) ) 			add_action( 'wp_head', array(&$this, 'recent_comments_style') ); 		add_action( 'comment_post', array(&$this, 'flush_widget_cache') ); 		add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') ); 	} 	function recent_comments_style() { 		if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876 			|| ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) ) 			return; 		?> 	<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style> <?php 	} 	function flush_widget_cache() { 		wp_cache_delete('effiwidget_recent_comments', 'widget'); 	} 	function widget( $args, $instance ) { 		global $comments, $comment; 		$cache = wp_cache_get('effiwidget_recent_comments', 'widget'); 		if ( ! is_array( $cache ) ) 			$cache = array(); 		if ( ! isset( $args['widget_id'] ) ) 			$args['widget_id'] = $this->id; 		if ( isset( $cache[ $args['widget_id'] ] ) ) { 			echo $cache[ $args['widget_id'] ]; 			return; 		}  		extract($args, EXTR_SKIP);  		$output = ''; 		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Comments' ) : $instance['title'], $instance, $this->id_base ); 		if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )  			$number = 5; 		$comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ); 		$output .= $before_widget; 		if ( $title ) 			$output .= $before_title . $title . $after_title; 		$output .= '<ul id="recentcomments">'; 		if ( $comments ) { 			foreach ( (array) $comments as $comment) { 				$output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), strip_tags(get_comment_author_link()), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>'; 			}  		} 		$output .= '</ul>'; 		$output .= $after_widget; 		echo $output; 		$cache[$args['widget_id']] = $output; 		wp_cache_set('effiwidget_recent_comments', $cache, 'widget'); 	} 	function update( $new_instance, $old_instance ) { 		$instance = $old_instance; 		$instance['title'] = strip_tags($new_instance['title']); 		$instance['number'] = absint( $new_instance['number'] ); 		$this->flush_widget_cache(); 		$alloptions = wp_cache_get( 'alloptions', 'options' ); 		if ( isset($alloptions['effiwidget_recent_comments']) ) 			delete_option('effiwidget_recent_comments'); 		return $instance; 	} 	function form( $instance ) { 		$title = isset($instance['title']) ? esc_attr($instance['title']) : ''; 		$number = isset($instance['number']) ? absint($instance['number']) : 5; ?> 		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> 		<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label> 		<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> <?php 	} } function widget_initeffiRecentComments() {     register_widget("WP_effiRecentCommentsWidget"); } add_action("widgets_init", "widget_initeffiRecentComments"); ?> Langage du code : PHP (php)

Vous pouvez plus simplement télécharger ce petit plugin / widget ici : effiRecentComments (vous avez juste à l’activer, il ajoutera un widget optimisé à votre site WordPress, que vous pourrez utiliser en lieu et place de celui de WordPress, mais en utilisant les styles CSS de ce dernier)

Voir tous les articles de la catégorie WordPress

1 réflexion sur “WordPress : un widget « Recent comments » moins spammy ?…”

  1. Avatar de Merci !

    Super ce partage c’est top !
    Et même une extension… que demander de plus.

    Encore merci, je vais tester ça.

    Répondre

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut