WordPress: filter the content of the comments

I've had to fix an annoying problem on a WordPress blog: users left comments with long sentences. This produced the release of the text, both in the backend, even worse, in the frontend. The effect was really horrible and to avoid hand-to moderate every single comment, the only solution was to filter out - somehow - the output of the comments.
Fortunately (and is one of the reason why I love all things WP) Wordpress allows you to add a filter to the output of the comments. In my case it was enough to add in the file functions.php the following lines of code:

1
2
3
4
5
6
7
8
9
10
11
12
$content ) { filter_comment function ($ content) {
explode ( " " , $content ) ; $ A = explode ("", $ content);
"" ; $ Content = "";
$a as $word ) { foreach ($ a as $ word) {
strlen ( $word ) > 20 ) $word = substr ( $word , 0 , 20 ) . "[t...]" ; if ( strlen ($ word)> 20) $ word = substr ($ word, 0, 20). "[t. ..]";
( $content == "" ) ? $word : ( " " . $word ) ; $ Content .= ($ content == "")? $ Word: ("." $ Word);
}
; return $ content;
}

, 'filter_comment' ) ; add_filter ('comment_text', 'filter_comment');
, 'filter_comment' ) ; add_filter ('get_comment_text', 'filter_comment');

The words are too long (over 20) are cut and, in the end, add [t...] - to indicate that a word is cut off.

2 comments to "WordPress: filter the content of comments"

  1. February 6, 2010 vik :

    Excuse that the difference between

    1
    comment_text

    and

    1
    get_comment_text

    and why use them both?

  2. February 7, 2010 Giovambattista Fazioli :

    e comment_text_rss . @ Vik: the current version of WordPress you can apply the filter to both functions, in truth, there is also get_comment_excerpt() , the comment_excerpt() and comment_text_rss .
    (come in molte altre funzioni scritte con questa nomenclatura) sta nel tipo di output: la prima, comment_text() , emette l'output a video (cioè esegue una echo interna) mentre la seconda, get_comment_text() , lo restituisce in una variabile, tipo: However, the difference between comment_text() and get_comment_text() (as in many other functions written in this nomenclature) is the type of output: the first, comment_text() , emits the screen output (ie running a echo inside) while the second, get_comment_text() , returns in a variable, type:

    1
    get_comment_text ( ) ; $ Comment = get_comment_text ();

    nel frontend o nei vari temi. In the example, when I posted both in get_comment_text() is largely used in the backend while the comment_text() in the frontend or in the various themes.
    . However, by performing a reverse engineer the code, it is clear that the function get_comment_text() is in entrocontennuta comment_text() . . It follows that it would be enough to apply the filter function only get_comment_text() to act - indirectly - also on the comment_text() .
    il nostro filtro non avrebbe effetto. Applying the filter to the sole function comment_text() , because there where we would have partial results is used get_comment_text() our filter would have no effect.
    In contrast, applying the filter to get_comment_text() - at least as it is written, the current engine of WordPress - it would act on entrabe functions.

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA