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.










Excuse that the difference between
and
and why use them both?
e
comment_text_rss. @ Vik: the current version of WordPress you can apply the filter to both functions, in truth, there is alsoget_comment_excerpt(), thecomment_excerpt()andcomment_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 unaechointerna) mentre la seconda,get_comment_text(), lo restituisce in una variabile, tipo: However, the difference betweencomment_text()andget_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 aechoinside) while the second,get_comment_text(), returns in a variable, type:nel frontend o nei vari temi. In the example, when I posted both in
get_comment_text()is largely used in the backend while thecomment_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 entrocontennutacomment_text(). . It follows that it would be enough to apply the filter function onlyget_comment_text()to act - indirectly - also on thecomment_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 usedget_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.