WordPress 所有外链在新标签打开

将下面的代码添加到主题的 function.php 中

function rt_add_link_target( $content ){
// use the  tag to split into segments
$bits = explode( '$bit ){
// fix the target="_blank" bug after the link
if ( strpos( $bit, 'href' ) === false ) continue;
// find the end of each link
$pos = strpos( $bit, '>' );
// check if there is an end (only fails with malformed markup)
if( $pos !== false ){
// get a string with just the link's attibutes
$part = substr( $bit, 0, $pos );
// for comparison, get the current site/network url
$siteurl = network_site_url();
// if the site url is in the attributes, assume it's in the href and skip, also if a target is present
if( strpos( $part, $siteurl ) === false && strpos( $part, 'target=' ) === false ){
// add the target attribute
$bits[$key] = 'target="_blank" ' . $bits[$key];
}
}
}
// re-assemble the content, and return it
return implode( '

typecho 文章外链自动添加nofollow和新窗口打开

nofollow标签是由谷歌领头创新的一个“反垃圾链接”的标签,并被百度、yahoo等各大搜索引擎广泛支持,引用nofollow标签的目的是:用于指示搜索引擎不要追踪(即抓取)网页上的带有nofollow属性的任何出站链接,以减少垃圾链接的分散网站权重。给文章的外链添加nofollow属性,可以有效防止垃圾链接对本站权重的稀释和伤害。

打开typecho跟目录下的 var/HyperDown.php 文件,找到:

return $self->makeHolder("<a href=\"{$url}\">{$escaped}</a>");

修改为:
if (strstr($url,'//你的网址') == false ) {

return $self->makeHolder("<a href=\"{$url}\" target=\"_blank\" rel=\"nofollow\">{$escaped}</a>");
} else {
return $self->makeHolder("<a href=\"{$url}\" target=\"_blank\">{$escaped}</a>");
}
退出移动版