Using WordPress ‘privacy_policy_url’ PHP filter

‘privacy_policy_url’ is a WordPress filter that allows you to modify the URL of the privacy policy page. It is useful when you want to change the default URL or implement a custom privacy policy page.

Usage

To use this filter, you need to add it to your theme’s functions.php file or a custom plugin.

add_filter( 'privacy_policy_url', 'my_custom_privacy_policy_url', 10, 2 );

function my_custom_privacy_policy_url( $url, $policy_page_id ) {
    // Your code to modify the URL
}

Parameters

  • $url (string): The URL to the privacy policy page. Empty string if it doesn’t exist.
  • $policy_page_id (int): The ID of the privacy policy page.

Examples

Change the privacy policy URL

add_filter( 'privacy_policy_url', 'change_privacy_policy_url', 10, 2 );

function change_privacy_policy_url( $url, $policy_page_id ) {
    return 'https://example.com/custom-privacy-policy';
}

This code changes the privacy policy URL to ‘https://example.com/custom-privacy-policy‘.

Add a query string to the privacy policy URL

add_filter( 'privacy_policy_url', 'add_query_string_to_privacy_policy_url', 10, 2 );

function add_query_string_to_privacy_policy_url( $url, $policy_page_id ) {
    return $url . '?utm_source=footer';
}

This code appends a query string ?utm_source=footer to the privacy policy URL.

Redirect the privacy policy page to another page

add_filter( 'privacy_policy_url', 'redirect_privacy_policy_page', 10, 2 );

function redirect_privacy_policy_page( $url, $policy_page_id ) {
    return home_url( '/terms-and-conditions' );
}

This code redirects the privacy policy page to the ‘Terms and Conditions’ page on your website.

Change the privacy policy URL based on the user’s language

add_filter( 'privacy_policy_url', 'multilanguage_privacy_policy_url', 10, 2 );

function multilanguage_privacy_policy_url( $url, $policy_page_id ) {
    $language = get_user_locale();
    if ( $language == 'fr_FR' ) {
        return 'https://example.com/fr/politique-confidentialite';
    } else {
        return 'https://example.com/en/privacy-policy';
    }
}

This code changes the privacy policy URL based on the user’s language. For French users, it shows the French privacy policy page, while for other users, it shows the English privacy policy page.

Add a custom URL for logged-in users

add_filter( 'privacy_policy_url', 'logged_in_user_privacy_policy_url', 10, 2 );

function logged_in_user_privacy_policy_url( $url, $policy_page_id ) {
    if ( is_user_logged_in() ) {
        return 'https://example.com/members-privacy-policy';
    } else {
        return $url;
    }
}

This code changes the privacy policy URL to a custom URL for logged-in users, while keeping the default URL for non-logged-in users.