将wordpress文章内的网址自动添加url

WordPress技术博客wp应用技术

wordpress文章内添加链接“电梯直达”?

我们在编辑网页的时候,很多的时候会引用一些链接,给用户一个“直达电梯”,点击即可到达引用的地方。

比如一些教程,或者一些其他的内容这都是非常常见的。

一般来说,我们输入带有http的链接,如果不手动进行添加的话,那么这个链接是不可点击的,因此,如果我们需要引用一个链接必须手动添加好。

这样一来就比较麻烦了,如果能够自动识别链接自动添加的话,对于我们编辑文章来说就非常方便了。

实际上,wordpress自己就提供了这个功能,只不过大部分的主题都没有开启而已,你可以输入以下代码开启这个功能,那么只要在文章内的链接都会自动识别并被加上url,不需要我们手动点击了。

将如下代码加入进入主题的function.php(WEB主题公园用户加入到function/function_z.php或者widget.php均可):

 

add_filter('the_content', 'make_clickable');

 

这样我们文章内的链接都会自动的添加链接可点击了,如果你的文章有外链,而不想要这些外链输出你的网站权重,那么可以加入如下代码,这样外链都会加入nofollow,不输出权重了:

 

add_filter( 'the_content', 'themepark_nofollow');
function themepark_nofollow( $content ) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !emptyempty($matches) ) {
$srcUrl = get_option('siteurl');
for ($i=0; $i < count($matches); $i++)
{
$tag = $matches[$i][0];
$tag2 = $matches[$i][0];
$url = $matches[$i][0];
$noFollow = '';
$pattern = '/target\s*=\s*"\s*_blank\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';
$pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' rel="nofollow" ';
$pos = strpos($url,$srcUrl);
if ($pos === false) {
$tag = rtrim ($tag,'>');
$tag .= $noFollow.'>';
$content = str_replace($tag2,$tag,$content);
}
}
}
}
$content = str_replace(']]>', ']]>', $content);
return $content;
}

上一篇:

下一篇:

文章评论

您好!请登录

取消回复
    展开更多