Using WordPress ‘get_privacy_policy_url()’ PHP function

The get_privacy_policy_url() WordPress PHP function retrieves the URL to the privacy policy page.

Usage

$privacy_policy_url = get_privacy_policy_url();

Parameters

  • None

More information

See WordPress Developer Resources: get_privacy_policy_url()

Examples

In this example, we’ll display the privacy policy link in the footer of a WordPress theme:

// Display Privacy Policy link in the footer
function display_privacy_policy_link() {
    $privacy_policy_url = get_privacy_policy_url();

    if ($privacy_policy_url) {
        echo '<a href="' . esc_url($privacy_policy_url) . '">Privacy Policy</a>';
    }
}
add_action('wp_footer', 'display_privacy_policy_link');

In this example, we’ll add the privacy policy link to a custom menu:

// Add Privacy Policy link to a custom menu
function add_privacy_policy_link($items, $args) {
    $privacy_policy_url = get_privacy_policy_url();

    if ($args->theme_location == 'primary' && $privacy_policy_url) {
        $items .= '<li><a href="' . esc_url($privacy_policy_url) . '">Privacy Policy</a></li>';
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'add_privacy_policy_link', 10, 2);

Check if Privacy Policy Page Exists

In this example, we’ll check if a privacy policy page exists before displaying its link:

// Check if Privacy Policy page exists
function check_privacy_policy_page_exists() {
    $privacy_policy_url = get_privacy_policy_url();

    if ($privacy_policy_url) {
        echo '<a href="' . esc_url($privacy_policy_url) . '">Privacy Policy</a>';
    } else {
        echo 'No Privacy Policy page found.';
    }
}

In this example, we’ll create a widget that displays the privacy policy link:

// Privacy Policy Link Widget
class Privacy_Policy_Link_Widget extends WP_Widget {
    function __construct() {
        parent::__construct('privacy_policy_link_widget', 'Privacy Policy Link');
    }

    function widget($args, $instance) {
        $privacy_policy_url = get_privacy_policy_url();
        if ($privacy_policy_url) {
            echo $args['before_widget'];
            echo '<a href="' . esc_url($privacy_policy_url) . '">Privacy Policy</a>';
            echo $args['after_widget'];
        }
    }
}
add_action('widgets_init', function() {
    register_widget('Privacy_Policy_Link_Widget');
});

In this example, we’ll create a shortcode that displays the privacy policy link:

// Privacy Policy Link Shortcode
function privacy_policy_link_shortcode() {
    $privacy_policy_url = get_privacy_policy_url();

    if ($privacy_policy_url) {
        return '<a href="' . esc_url($privacy_policy_url) . '">Privacy Policy</a>';
    } else {
        return 'No Privacy Policy page found.';
    }
}
add_shortcode('privacy_policy_link', 'privacy_policy_link_shortcode');