函数文档

get_privacy_policy_url()

💡 云策文档标注

概述

get_privacy_policy_url() 函数用于获取 WordPress 网站隐私政策页面的 URL。它通过读取 wp_page_for_privacy_policy 选项,检查页面状态并返回永久链接,若页面不存在则返回空字符串。

关键要点

  • 函数返回隐私政策页面的 URL 字符串,若页面未设置或未发布则返回空字符串。
  • 内部通过 get_option('wp_page_for_privacy_policy') 获取页面 ID,并使用 get_permalink() 生成链接。
  • 提供 privacy_policy_url 过滤器钩子,允许开发者自定义 URL 输出。
  • 自 WordPress 4.9.6 版本引入,常用于导航菜单、用户通知等场景。

代码示例

// 基本用法示例:获取隐私政策链接
$privacy_policy_page_link = get_the_privacy_policy_link();
// 变量可能包含类似:<a href="http://www.wpdev.local/politica-privacidad/" rel="nofollow ugc">Política de privacidad</a>

// 进阶示例:分离存储页面信息
$policy_page_title = '';
$policy_page_url = '';
$policy_page_id = (int) get_option('wp_page_for_privacy_policy');
if ($policy_page_id && get_post_status($policy_page_id) === 'publish') {
    $policy_page_title = get_the_title($policy_page_id);
    $policy_page_url = get_permalink($policy_page_id);
}

注意事项

  • 确保隐私政策页面已设置并发布,否则函数返回空字符串。
  • 使用 privacy_policy_url 过滤器可灵活调整 URL,例如添加查询参数或重定向。
  • 相关函数包括 get_post_status()、get_permalink()、apply_filters() 和 get_option(),用于内部实现。

📄 原文内容

Retrieves the URL to the privacy policy page.

Return

string The URL to the privacy policy page. Empty string if it doesn’t exist.

Source

function get_privacy_policy_url() {
	$url            = '';
	$policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );

	if ( ! empty( $policy_page_id ) && get_post_status( $policy_page_id ) === 'publish' ) {
		$url = (string) get_permalink( $policy_page_id );
	}

	/**
	 * Filters the URL of the privacy policy page.
	 *
	 * @since 4.9.6
	 *
	 * @param string $url            The URL to the privacy policy page. Empty string
	 *                               if it doesn't exist.
	 * @param int    $policy_page_id The ID of privacy policy page.
	 */
	return apply_filters( 'privacy_policy_url', $url, $policy_page_id );
}

Hooks

apply_filters( ‘privacy_policy_url’, string $url, int $policy_page_id )

Filters the URL of the privacy policy page.

Changelog

Version Description
4.9.6 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    /* Basic example */
    /* Get the privacy policy page link*/
    // We store the privacy policy link in a variable
    $privacy_policy_page_link = get_the_privacy_policy_link();
    
    // Our variable will contain something like this
    // <a class="privacy-policy-link" href="<a href="http://www.wpdev.local/politica-privacidad/">Política" rel="nofollow ugc">http://www.wpdev.local/politica-privacidad/">Política</a> de privacidad</a>
    // Now we can work with this link

  2. Skip to note 6 content

    // Save privacy page info into separate variables
    $policy_page_title  = '';
    $policy_page_url    = '';
    $policy_page_id     = (int) get_option( 'wp_page_for_privacy_policy' );
    
    if ( $policy_page_id && get_post_status( $policy_page_id ) === 'publish' ) {
        $policy_page_title  = get_the_title( $policy_page_id );
        $policy_page_url    = get_permalink( $policy_page_id );
    }
    // Example Uses:
    
    <a href="<?php echo esc_url(get_permalink($policy_page_id)) ?>" target="_blank"></a>