假设你的目标是帖子内容中的链接,我会这样说:
add_filter( \'the_content\', \'wpse_254317_nofollow\' );
/**
* Add rel nofollow according to a predefined domain
* to links in content
* @param $content
* @link http://stackoverflow.com/a/4809181/1930236
* @return mixed
*/
function wpse_254317_nofollow( $content ) {
$my_folder = "https://google.com";
preg_match_all( \'~<a.*>~isU\', $content, $matches );
for ( $i = 0; $i <= sizeof( $matches[0] ); $i ++ ) {
if ( isset( $matches[0][ $i ] ) && ! preg_match( \'~nofollow~is\', $matches[0][ $i ] )
&& ( preg_match( \'~\' . preg_quote( $my_folder ) . \'~\', $matches[0][ $i ] )
|| ! preg_match( \'~\' . get_bloginfo( \'url\' ) . \'~\', $matches[0][ $i ] ) )
) {
$result = trim( $matches[0][ $i ], ">" );
$result .= \' rel="nofollow">\';
$content = str_replace( $matches[0][ $i ], $result, $content );
}
}
return $content;
}
代码匹配帖子内容中的所有链接,并为设置为$my\\u文件夹的域添加所需的rel,该文件夹为“
https://google.com“在我的例子中。