永久地为外部链接添加rel =“nofollow”和target =“_ blank”

时间:2015-05-18 22:14:29

标签: wordpress

我想永久地为我的Wordpress帖子和页面中的所有外部链接添加rel =“nofollow”和target =“_ blank”。我知道,有插件,它们也是这样做的,但是一旦它们被禁用,所有的更改都将被撤销,文章从一开始就是相同的。

我不知道如何区分内部或外部链接,也不知道如何检查是否已存在rel =“nofollow”或target =“_ blank”属性。

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

是的,有人能帮帮我吗?感谢您的支持。

4 个答案:

答案 0 :(得分:5)

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

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",以避免对域名进行硬编码。

代码经过测试且有效。 希望这个有所帮助。

答案 1 :(得分:1)

您可以使用以下代码段: http://wpsnipp.com/index.php/functions-php/nofollow-external-links-only-the_content-and-the_excerpt/

  

这个很棒的小片段会将rel =“nofollow”添加到外部   the_content和the_excerpt中的链接。将此代码段添加到   你的wordpress主题的functions.php启用nofollow外部   链接。

add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');
function my_nofollow($content) {
    return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
function my_nofollow_callback($matches) {
    $link = $matches[0];
    $site_link = get_bloginfo('url');
    if (strpos($link, 'rel') === false) {
        $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
    } elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
        $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
    }
    return $link;
}

答案 2 :(得分:0)

我认为将rel"nofollow"target="_blank"添加到外发链接永久比在此处显示更多的工作。您必须重建External Links等插件的功能,以便甚至可以重写wp_nav_menus中的链接。

我有一个建议,在加载页面时通过JavaScript添加所需的属性。您可以将此脚本直接添加到主题标题中,也可以将其保存在主题为“functions.php

的脚本中的单独文件中
$(document).ready(function () {
    $( "a:not(a[href^='http://www.your-domain-name.com'],a[href^='javascript'],a[href^='#'])" ).attr({
                rel: "nofollow",
                target: "_blank"
            });
});

答案 3 :(得分:0)

我回答了@ rony-samuel,并调整了一些您可能觉得有用的东西。

使用内置的make_clickable函数自动包装链接。 (例如,通过API创建帖子时很有用)-然后检查用户是否已向链接中添加了其他类(例如button以具有不同的样式)–我们不想覆盖该内容,因此只需返回给定的标记为$m[0]

最后是正则表达式。与make_clickable结合使用时,它将输出<a <a href...,因此链接中的链接。我更正了正则表达式以避免这种情况。

// Auto warp links within content
add_filter( 'the_content', 'make_clickable' );

// Add target blank and nofollow to external links
// Do not overwrite links that probably have been placed manually in the content
// and contain classes like "button" or whatever etc. Since they were placed manually
// with additional styling, the editor can add target="_blank" manually as well if needed.

function external_links ($content) {
    $content = preg_replace_callback(
    '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
    function($m) {
        $hasClass = (bool) preg_match('/class="[^"]*[^"]*"/', $m[0]);

        if (strpos($m[1], home_url()) === false && $hasClass === false)
            return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
        else
            return $m[0];
        },

        $content);
        return $content;
    }

// set a very low priority to ensure,
// all the content and shortcode things has been completed already
add_filter('the_content', 'external_links', 999);
相关问题