Добрый день!
Обрезаю длинные URL в комментариях следующим плагином:
<?php
/*
Plugin Name: Chunk Urls for WordPress
Plugin URI: http://www.village-idiot.org/archives/2006/06/29/wp-chunk/
Description: This plugin shorten urls in comments so that they won’t break your site.
Author: whoo
Version: 2.0
Author URI: http://www.village-idiot.org/
*/
function make_chunky($ret)
{
// pad it with a space
$ret = ‘ ‘ . $ret;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "$1<a href=’$2′ rel=’nofollow’>$2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "$1<a href=’http://$2′ rel=’nofollow’>$2</a>", $ret);
//chunk those long urls
chunk_url($ret);
$ret = preg_replace("#(\s)([a-z0-9\-_.]+)@([^,< \n\r]+)#i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $ret);
// Remove our padding..
$ret = substr($ret, 1);
return($ret);
}
function chunk_url(&$ret)
{
$links = explode(‘<a’, $ret);
$countlinks = count($links);
for ($i = 0; $i < $countlinks; $i++)
{
$link = $links[$i];
$link = (preg_match(‘#(.*)(href=")#is’, $link)) ? ‘<a’ . $link : $link;
$begin = strpos($link, ‘>’) + 1;
$end = strpos($link, ‘<‘, $begin);
$length = $end – $begin;
$urlname = substr($link, $begin, $length);
/*
* We chunk urls that are longer than 50 characters. Just change
* ’50’ to a value that suits your taste. We are not chunking the link
* text unless if begins with ‘http://’, ‘ftp://’, or ‘www.’
*/
$chunked = (strlen($urlname) > 50 && preg_match(‘#^(http://|ftp://|www\.)#is’, $urlname)) ? substr_replace($urlname, ‘…..’, 30, -10) : $urlname;
$ret = str_replace(‘>’ . $urlname . ‘<‘, ‘>’ . $chunked . ‘<‘, $ret);
}
}
remove_filter(‘comment_text’, ‘make_clickable’);
add_filter(‘comment_text’, ‘make_chunky’);
?>
Вывожу список последних комментариев в сайдбаре следующим плагином:
<?php
/*
Plugin Name: Recent Comments
Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/
Description: Retrieves a list of the most recent comments.
Version: 1.18
Author: Nick Momrik
Author URI: http://mtdewvirus.com/
*/
function mdv_recent_comments($no_comments = 5, $comment_lenth = 5, $before = ‘<li>’, $after = ‘</li>’, $show_pass_post = false, $comment_style = 0) {
global $wpdb;
$request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN (‘publish’,’static’) ";
if(!$show_pass_post) $request .= "AND post_password =” ";
$request .= "AND comment_approved = ‘1’ ORDER BY comment_ID DESC LIMIT $no_comments";
$comments = $wpdb->get_results($request);
$output = ”;
if ($comments) {
foreach ($comments as $comment) {
$comment_author = stripslashes($comment->comment_author);
if ($comment_author == "")
$comment_author = "anonymous";
$comment_content = strip_tags($comment->comment_content);
$comment_content = stripslashes($comment_content);
$words=split(" ",$comment_content);
$comment_excerpt = join(" ",array_slice($words,0,$comment_lenth));
$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
if ($comment_style == 1) {
$post_title = stripslashes($comment->post_title);
$url = $comment->comment_author_url;
if (empty($url))
$output .= $before . $comment_author . ‘ on ‘ . $post_title . ‘.’ . $after;
else
$output .= $before . "<a href=’$url’ rel=’external’>$comment_author</a>" . ‘ on ‘ . $post_title . ‘.’ . $after;
}
else {
$output .= $before . ‘<strong>’ . $comment_author . ‘:</strong> <a href="’ . $permalink;
$output .= ‘" title="View the entire comment by ‘ . $comment_author.’">’ . $comment_excerpt.'</a>’ . $after;
}
}
$output = convert_smilies($output);
} else {
$output .= $before . "None found" . $after;
}
echo $output;
}
?>
В комментариях длинные URL обрезаются, а в списке последних – нет. Подскажите, пожалуйста, где поправить, чтобы длинные URL обрезались и в списке последних комментариев.
Смотрим /wp-includes/comment-template.php и видим:
function comment_text() {
echo apply_filters(‘comment_text’, get_comment_text() );
}
Фильтр на comment_text определен в плагине Chunk Urls for WordPress
А в Recent Comments я не вижу применения фильтра.
Ю.Б., спасибо за указание на дверь, вошёл удачно. 😉