函数文档

wp_rel_nofollow()

💡 云策文档标注

概述

wp_rel_nofollow() 是一个 WordPress 函数,用于在内容中的所有 HTML A 元素中添加 rel="nofollow" 属性。它作为预保存过滤器运行,处理转义和反转义以确保数据安全。

关键要点

  • 函数作用:自动为内容中的链接添加 rel="nofollow" 属性,常用于 SEO 优化以防止传递链接权重。
  • 参数:接受一个字符串参数 $text,代表可能包含 HTML A 元素的内容。
  • 返回值:返回转换后的字符串内容。
  • 内部机制:使用 preg_replace_callback 和 wp_rel_callback 进行正则匹配和属性添加,并处理 slashes 以确保数据完整性。
  • 相关函数:wp_rel_callback() 用于回调添加 rel 属性,wp_slash() 用于添加 slashes。
  • 版本历史:自 WordPress 1.5.0 版本引入。

代码示例

function wp_rel_nofollow( $text ) {
    // This is a pre-save filter, so text is already escaped.
    $text = stripslashes( $text );
    $text = preg_replace_callback(
        '||i',
        static function ( $matches ) {
            return wp_rel_callback( $matches, 'nofollow' );
        },
        $text
    );
    return wp_slash( $text );
}

📄 原文内容

Adds rel="nofollow" string to all HTML A elements in content.

Parameters

$textstringrequired
Content that may contain HTML A elements.

Return

string Converted content.

Source

function wp_rel_nofollow( $text ) {
	// This is a pre-save filter, so text is already escaped.
	$text = stripslashes( $text );
	$text = preg_replace_callback(
		'|<a (.+?)>|i',
		static function ( $matches ) {
			return wp_rel_callback( $matches, 'nofollow' );
		},
		$text
	);
	return wp_slash( $text );
}

Changelog

Version Description
1.5.0 Introduced.