How to filter privacy policy link function in wordpress?

wordpress filter function

the_privacy_policy_link() is a built-in WordPress function introduced in WordPress 4.9.6 as part of the GDPR (General Data Protection Regulation) compliance features.

🔍 Purpose:

It displays a link to your site’s Privacy Policy page, if one is set in the WordPress settings.


✅ Syntax:

the_privacy_policy_link( $before = '', $after = '' );

Parameters:

  • $before (string) – Optional. Text or HTML to display before the link.
  • $after (string) – Optional. Text or HTML to display after the link.

✅ Example Usage:

<?php the_privacy_policy_link( '<p>', '</p>' ); ?>

This will output something like:

<p><a href="https://example.com/privacy-policy/">Privacy Policy</a></p>

Only if a Privacy Policy page has been set in Settings → Privacy.


✅ How to Set the Privacy Policy Page:

  1. Go to WordPress Dashboard → Settings → Privacy
  2. Select an existing page or create a new one for your Privacy Policy
  3. WordPress will now recognize that as the privacy policy page for functions like the_privacy_policy_link().

🧠 Pro Tip:

You can use this function in your footer or anywhere you’d like to automatically display a privacy policy link — useful for themes, plugins, and compliance purposes.

You can create a filter-based solution that appends your custom link after the default privacy policy link, while preserving WordPress core functionality.

Since the_privacy_policy_link() internally uses get_the_privacy_policy_link() (which is filterable), you can hook into it.


✅ Add this to your functions.php:

phpCopyEditadd_filter( 'the_privacy_policy_link', 'add_custom_link_after_privacy_policy', 10, 2 );

function add_custom_link_after_privacy_policy( $link, $privacy_policy_url ) {
$custom_link = ' <a href="#">Custom Link</a>';
return $link . $custom_link;
}

✅ Output in template file:

You can keep using this in your theme file:

if ( function_exists( 'the_privacy_policy_link' ) ) {
the_privacy_policy_link( '<p class="privacy-policy">', '</p>' );
}

📝 Result:

If your privacy policy is set, it will output:

<p class="privacy-policy">
<a href="https://yourdomain.com/privacy-policy/">Privacy Policy</a>
<a href="#">Custom Link</a>
</p>

Leave a Comment

Your email address will not be published. Required fields are marked *