在看文章之前请确定你已明确标题意思:是评论内容里的超链接,而非评论者的网站链接。
自上次解决了《两个遗留问题》之一的《win主机下代码配置wordpress的SMTP邮件发送》后,本问题迟迟得不到解决(不用编辑器、不用a标签),最后不了了之,索性屏蔽了评论内容里的链接的超链转换。而就在刚才,这个问题终于得到解决。
找到 wp-includes\formatting.php
搜索 function _make_url_clickable_cb
会看到以下代码:
- function _make_url_clickable_cb($matches) {
- $url = $matches[2];
- if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
- // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
- // Then we can let the parenthesis balancer do its thing below.
- $url .= $matches[3];
- $suffix = '';
- } else {
- $suffix = $matches[3];
- }
- // Include parentheses in the URL only if paired
- while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
- $suffix = strrchr( $url, ')' ) . $suffix;
- $url = substr( $url, 0, strrpos( $url, ')' ) );
- }
- $url = esc_url($url);
- if ( emptyempty($url) )
- return $matches[0];
- return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a> " . $suffix;
- }
然后修改其中:
- return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a> " . $suffix;
修改为:
- return $matches[1] . "<a href=\"$url\" rel=\"nofollow\" target=\"_blank\">$url</a> " . $suffix;
这样,评论内容中的超链接就能在新窗口打开了。