添加rel =“nofollow"和目标=“_blank"永久用于外部链接 [英] Add rel="nofollow" and target="_blank" for external links permanently

查看:30
本文介绍了添加rel =“nofollow"和目标=“_blank"永久用于外部链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的 Wordpress 帖子和页面中的所有外部链接永久添加 rel="nofollow" 和 target="_blank".我知道有一些插件可以做同样的事情,但是一旦它们被禁用,所有更改都将被撤销,文章与开头相同.

I would like to add rel="nofollow" and target="_blank" for all external links in my Wordpress posts and pages permanently. I am aware, that there are plugins, which do the same, but as soon as they get disabled, all changes will be reversed and the articles are the same as from the beginning.

我不知道如何区分内部链接或外部链接,也不知道如何检查是否已经存在 rel="nofollow" 或 target="_blank" 属性.

I do not know how to differ between internal or external links nor how to check if there is already a rel="nofollow" or target="_blank" attribute.

我想最好的方法是使用 PHP 而不是 MySQL.我已经在网上搜索了指南、教程或插件,但没有成功.

I guess the best way of doing this would be using PHP instead of MySQL. I already searched the web for guides, tutorials or plugins, without success.

有人可以帮助我吗?感谢您的支持.

May someone help me? I appreciate your support.

推荐答案

我有一个将 nofollow 应用到所有现有和新外部链接的解决方案.将代码复制到激活主题的functions.php中

I have got a solution for applying nofollow to all existing and new external links. Copy the code into your functions.php of your activated theme

function add_nofollow_content($content) {
$content = preg_replace_callback('/]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i', function($m) {
    if (strpos($m[1], "YOUR_DOMAIN_ADDRESS") === false)
        return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
    else
        return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
    }, $content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');

您也可以在提供的空间中调用函数home_url() 而不是"YOUR_DOMAIN_ADDRESS",以避免对域名进行硬编码.

You can also call the function home_url() instead of "YOUR_DOMAIN_ADDRESS" in the space provided to avoid hard coding of the domain name.

代码已经过测试并且可以正常工作.希望这个有帮助.

The code is tested and it works. Hope this one helps.

这篇关于添加rel =“nofollow"和目标=“_blank"永久用于外部链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆