零基础建站培训介绍

当前位置:

WordPress 实现文章链接新窗口打开,站外链接自动加nofollow

以下是Wordpress做网站过程中常用的几个功能,分享给学习建网站的新手们。

功能一:文章内容全部链接新窗口打开


function Bing_autoblank($text) {
$return = str_replace('<a','<a target="_blank"',$text);
return $return;
}
if( Bing_get_panel('post_url_blank') ) add_filter('the_content','Bing_autoblank');

功能二:文章内容外链自动添加 nofollow 并在新窗口打开


function Bing_nf_url_parse($content){
$regexp = "<a\s[^>]*href=("??)([^" >]*?)\\1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)){
if(!empty($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;
}
if( Bing_get_panel('post_url_blank_nofollow') ) add_filter('the_content','Bing_nf_url_parse');

功能三:评论作者链接新窗口打开


function Bing_comment_author_link() {
$url = get_comment_author_url( $comment_ID );
$author = get_comment_author( $comment_ID );
if ( empty( $url ) || 'http://' == $url ) return $author;
return '<a href="'. $url .'" rel="external nofollow" target="_blank" class="url">'.$author.'</a>';
}
add_filter('get_comment_author_link', 'Bing_comment_author_link');
上一课: 下一课:

相关教程

  • sql server软件安装完毕后,需要新建一个数据库用来作为做网站的数据库。下面介绍一下sqlserver数据库如何创建用户的流程。1、打开sql管理界面,如图
  • bloginfo() 直接在浏览器中输出内容,我们创建一个wordpress博客的时候,我们需要填写博客的相关信息,包括博客名称,博客描述,博客地址等等。当我们
  • 以下是5个在Wordpress 搜索功能开发过程中用到的小功能代码。第一个:搜索结果只有一篇文章时自动跳转到文章function Bing_redirect_single_post&
  • 织梦数据库配置文件所在位置路径为:/data/common.inc.php修改方法:把这个文件使用ftp下载下来,用记事本编辑.下面是该dede数据库配置文件的内容:
  • 用织梦dedecms建站的新手站长,在更新文章图片时都会添加自己网站独特的水印,织梦dedecms默认安装后,用的是默认水印,很不美观。其实解决水印控制
  • 站群主要包括子域名站群、独立域名站群、目录站群等多种解决方案,优势各有千秋,站群软件也有多种。以下是给大家介绍使用最常见的CMS系统-织梦dede
  • 很多人都是用dedecms(织梦) 源码建设网站,有些时候网站需要开启会员功能才能使用一些功能,那么dedecms(织梦)如何开启会员功能呢?下面就跟大家
  • dedecms中include标签的作用是引入一个文件。代码形式:{dede:include filename="文件路径"/}这个标签可以很好的帮助我们
  • 原因:DEDECMS 织梦后台验证码不显示主要原因是由于网站配置文件包含Unicode签名。解决方法:一般上传到服务器上配置文件是需要修改下的,很多程序
  • dedecms怎么控制文章摘要字数有时候觉得文字的摘要不够长,我们经常的操作就是去后台修改文字字数,但是织梦默认最大的摘要长度是250个字符,2个字符