30. September 2011
von Blackbam
Recently we had an issue with the line-breaks in WordPress comments - the comments are saved to the database as they are submitted from the textarea in your theme, and depending on the theme so they are displayed. If your comments are displayed as HTML and there is no mechanism to add line breaks anywhere, then the comments do not have any line-breaks (as HTML line breaks must be created with a br-tag or something similar). Another anoying thing is when commenters do not make links to hyperlinks, as these are not linked and clickable afterwards. We have found a solution for these problem - just copy the following script to your functions.php-file and your comments will be sanitized: 
add_filter('preprocess_comment','format_the_comment_html');
 
function format_the_comment_html($comment_data) {
 $comment_data['comment_content']=nl2br(makeClickableLinks($comment_data['comment_content']));
 return $comment_data;
}
 
function makeClickableLinks($text) {
 $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a href="\\1">\\1</a>', $text);
 $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a href="http://\\2">\\2</a>', $text);
 $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</a>', $text);
 return $text;
}
Share

Dieser Eintrag wurde am 30. September 2011 um 14:14 in der Kategorie WordPress, WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags: , , ,

No comments yet

Kommentare abonnieren (RSS) or URL Trackback

Leave a comment: