Sbarazzarsi del codice HTML all’interno dei commenti in WordPress

18 settembre 2010 17:22

php

Come avrete già intuito dal titolo, in questo tutorial vedremo come eliminare il codice HTML presente all‘interno dei commenti.

Il codice HTML nei commenti può essere un bene, ma molte volte le persone ne abusano, per esempio inserendo link. In questa articolo, vi mostrerò come è possibile sbarazzarsi automaticamente di qualsiasi HTML presente nei commenti del vostro post.

Copiate e incollate il seguente codice all’interno del file functions.php

// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
 
	// convert everything in a comment to display literally
	$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
 
	// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
	$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
 
	return( $incoming_comment );
}
 
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
 
	// Put the single quotes back in
	$comment_to_display = str_replace( ''', "'", $comment_to_display );
 
	return $comment_to_display;
}
 
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);

Post correlati

You can leave a response, or trackback from your own site.
Tags: ,

Leave a Reply