NoFollow Rel =“ sponsored” pre_replace

问题描述 投票:0回答:1

我使用wordpress,我在我的functions.php中添加了一些代码,以自动将所有不符合某些条件的链接作为非跟随链接。

当前,如果链接examplesite.comexamplesite2.com标记为dofollow,则设置为nofollow

我用来实现此目的(完美运行)的代码是:

function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === 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');

但是,我想添加一个附加条件。

如果站点是examplesiteexamplesite2.com,则将其标记为rel="follow"

elseif

如果是examplesite3.comexamplesite4.com,则将其标记为rel=”sponsored”

否则(如果不满足任何一个条件,则返回

no Follow代替。

有人可以帮我吗?我尝试添加

function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false && strpos($m[1], "https://examplesite3.com") === false && strpos($m[3], "https://examplesite4.com") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
return '<a href="'.$m[1].'" rel="sponsored" target="_blank">'.$m[3].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');

但是它创建了一个循环,但是没有用。预先感谢您的任何想法!

而且我也尝试过这个,改变了elseif,但它似乎没有超过第二个条件。

function add_nofollow_content($content) {
$content = preg_replace_callback(
'/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
function($m) {
if (strpos($m[1], "https://www.examplesite.com") === false && strpos($m[1], "https://www.examplesite2.com") === false && strpos($m[3], "https://www.bluehost.com") === false && strpos($m[3], "https://amzn.to") === false)
return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
elseif (strpos($m[3], "https://www.examplesponsor.com") === false && strpos($m[3], "https://www.examplesponsor2.com") === false && strpos($m[3], "https://examplesponsor3.com") === false)
return '<a href="'.$m[3].'" rel="sponsored" target="_blank">'.$m[4].'</a>';
        else
return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
},
$content);
return $content;
}
add_filter('the_content', 'add_nofollow_content');
wordpress preg-replace nofollow
1个回答
0
投票

尝试这样:

function add_nofollow_content($content) {
  $content = preg_replace_callback(
    '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
    function($m) {
      if ((strpos($m[1], "https://www.examplesite.com") !== false) ||
          (strpos($m[1], "https://www.examplesite2.com") !== false)) {
        return '<a href="'.$m[1].'" rel="follow" target="_blank">'.$m[2].'</a>';
      } elseif ((strpos($m[1], "https://www.examplesite3.com") !== false) ||
                (strpos($m[1], "https://www.examplesite4.com") !== false)) {
        return '<a href="'.$m[1].'" rel="sponsored" target="_blank">'.$m[2].'</a>';
      } else {
        return '<a href="'.$m[1].'" rel="nofollow" target="_blank">'.$m[2].'</a>';
      }
    },
    $content);
  return $content;
}
add_filter('the_content', 'add_nofollow_content');

这将检查是否使用strpos找到两个URL中的任何一个,并为每种情况返回适当的rel=。对于所有其他URL,它将返回rel="nofollow"。如果您必须定义更多URL,请确保也按逻辑||对其进行设置。

对于更长的URL列表,我将更改代码结构以进行URL的数组映射→rel类型,然后将匹配每个条目的条目循环到匹配项。

© www.soinside.com 2019 - 2024. All rights reserved.