【问题标题】:Add rel="nofollow" and target="_blank" for external links permanently为外部链接永久添加 rel="nofollow" 和 target="_blank"
【发布时间】:2020-07-27 20:09:29
【问题描述】:

我想为我的 Wordpress 帖子和页面中的所有外部链接永久添加 rel="nofollow" 和 target="_blank"。我知道,有些插件也有同样的作用,但一旦它们被禁用,所有更改都将被撤消,并且文章与开始时相同。

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

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

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

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我有一个解决方案,可以将 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",以避免对域名进行硬编码。

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

    【讨论】:

    • 为什么else上面还有target_blank
    【解决方案2】:

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

    这个很棒的小 sn-p 会将 rel=”nofollow” 添加到外部 the_content 和 the_excerpt 中的链接。将此 sn-p 添加到 您的 wordpress 主题的 functions.php 以启用 nofollow external 链接。

    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;
    }
    

    【讨论】:

    • 这似乎工作正常,但我倾向于将所有内部链接编码为&lt;a href="/parent/child-item/",您的代码会将其识别为外部链接。这如何适用于这些相对 URL?
    【解决方案3】:

    我认为将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"
                });
    });
    

    【讨论】:

      【解决方案4】:

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

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

      最后一件事是正则表达式。结合make_clickable,它将输出&lt;a &lt;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);
      

      【讨论】:

        猜你喜欢
        • 2019-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-29
        • 1970-01-01
        • 2012-08-16
        • 2011-03-23
        • 1970-01-01
        相关资源
        最近更新 更多