函数文档

get_the_privacy_policy_link()

💡 云策文档标注

概述

get_the_privacy_policy_link() 函数用于返回格式化后的隐私政策链接,仅在隐私政策页面存在时生效。它接受前后缀参数,并可通过过滤器进行自定义。

关键要点

  • 函数返回隐私政策链接的 HTML 标记,若链接不存在则返回空字符串。
  • 参数 $before 和 $after 用于在链接前后添加自定义内容,默认均为空。
  • 内部调用 get_privacy_policy_url() 和 get_the_title() 获取 URL 和页面标题,并使用 esc_url() 和 esc_html() 进行转义。
  • 提供 the_privacy_policy_link 过滤器,允许开发者修改链接输出。
  • 从 WordPress 4.9.6 版本引入,6.2.0 版本添加了 'privacy-policy' rel 属性。

代码示例

// 添加隐私政策链接
echo get_the_privacy_policy_link();

// 在 p 标签内添加隐私政策链接
echo get_the_privacy_policy_link( '<p>', '</p>' );

// 另一种显示方式
$policy_link = get_the_privacy_policy_link();
echo $policy_link;

📄 原文内容

Returns the privacy policy link with formatting, when applicable.

Parameters

$beforestringoptional
Display before privacy policy link. Default empty.
$afterstringoptional
Display after privacy policy link. Default empty.

Return

string Markup for the link and surrounding elements. Empty string if it doesn’t exist.

Source

function get_the_privacy_policy_link( $before = '', $after = '' ) {
	$link               = '';
	$privacy_policy_url = get_privacy_policy_url();
	$policy_page_id     = (int) get_option( 'wp_page_for_privacy_policy' );
	$page_title         = ( $policy_page_id ) ? get_the_title( $policy_page_id ) : '';

	if ( $privacy_policy_url && $page_title ) {
		$link = sprintf(
			'<a class="privacy-policy-link" href="%s" rel="privacy-policy">%s</a>',
			esc_url( $privacy_policy_url ),
			esc_html( $page_title )
		);
	}

	/**
	 * Filters the privacy policy link.
	 *
	 * @since 4.9.6
	 *
	 * @param string $link               The privacy policy link. Empty string if it
	 *                                   doesn't exist.
	 * @param string $privacy_policy_url The URL of the privacy policy. Empty string
	 *                                   if it doesn't exist.
	 */
	$link = apply_filters( 'the_privacy_policy_link', $link, $privacy_policy_url );

	if ( $link ) {
		return $before . $link . $after;
	}

	return '';
}

Hooks

apply_filters( ‘the_privacy_policy_link’, string $link, string $privacy_policy_url )

Filters the privacy policy link.

Changelog

Version Description
6.2.0 Added 'privacy-policy' rel attribute.
4.9.6 Introduced.

User Contributed Notes