Get the privacy policy link in WordPress with get_the_privacy_policy_link

The get_the_privacy_policy_link function in WordPress is designed to retrieve the privacy policy page link for a website. This function is part of WordPress’s privacy tools that were introduced in version 4.9.6. The function checks if a privacy policy page is set in the website’s settings and if it exists, the function will return the HTML link to that page.

From a functional standpoint, this function aids in the display of the privacy policy link in a consistent manner across the website. This can contribute to the overall user experience by providing easy access to the privacy policy page. Additionally, it helps in ensuring that the website is in compliance with laws and regulations that require the presence of a privacy policy.

It is important to note that if no privacy policy page is set in the settings, or if the page does not exist, the function will return an empty string.

Parameters

The get_the_privacy_policy_link function in WordPress accepts two parameters. These are:

  • $before (string): This parameter is optional. The default value is an empty string (”). It represents the text or HTML that will be displayed before the privacy policy link.
  • $after (string): This parameter is also optional, with the default value as an empty string (”). It signifies the text or HTML that will be displayed after the privacy policy link.

Return Value

The get_the_privacy_policy_link function returns a string. This string contains the markup for the link and the surrounding elements. If the privacy policy link does not exist, the function will return an empty string.

Examples

How to Display the Privacy Policy Link

if ( function_exists( 'get_the_privacy_policy_link' ) ) {
 echo get_the_privacy_policy_link();
}

This code checks if the get_the_privacy_policy_link function exists, and if it does, it echoes (displays) the privacy policy link on the page.

How to Display the Privacy Policy Link with Text Before and After

if ( function_exists( 'get_the_privacy_policy_link' ) ) {
 echo get_the_privacy_policy_link( 'Before: ', ' After' );
}

In this example, the get_the_privacy_policy_link function is used with the optional parameters $before and $after. The text ‘Before: ‘ is displayed before the privacy policy link and the text ‘ After’ is displayed after the link.

How to Use the Privacy Policy Link in a Conditional Statement

if ( function_exists( 'get_the_privacy_policy_link' ) ) {
 $privacy_link = get_the_privacy_policy_link();
 if ( $privacy_link ) {
 echo "<p>Our Privacy Policy: $privacy_link</p>";
 } else {
 echo "<p>We do not have a Privacy Policy page.</p>";
 }
}

This code first checks if the get_the_privacy_policy_link function exists. If it does, it gets the privacy policy link and checks if the link exists. If the link exists, it is displayed with the text ‘Our Privacy Policy: ‘. If the link does not exist, a message is displayed stating ‘We do not have a Privacy Policy page.’

Conclusion

The get_the_privacy_policy_link function in WordPress is an essential tool for retrieving the privacy policy link of a website. This function is particularly useful in instances where the privacy policy page needs to be linked from various parts of the website, such as the footer or the sidebar. By calling this function, developers can ensure that the correct and most up-to-date privacy policy link is always displayed, thus maintaining the site’s compliance with privacy regulations.

Related WordPress Functions