Using WordPress ‘privacy_ping_filter()’ PHP function

The privacy_ping_filter() WordPress PHP function checks whether a blog is public before returning sites.

Usage

$sites = privacy_ping_filter($sites);

Parameters

  • $sites (mixed) – Required. The list of sites that will be filtered based on their public status.

More information

See WordPress Developer Resources: privacy_ping_filter()

Examples

Filter a list of sites

This example filters a list of sites and only returns public ones.

// An array of site IDs
$site_ids = array(1, 2, 3, 4, 5);

// Get site objects
$sites = array_map('get_site', $site_ids);

// Filter sites to only include public ones
$public_sites = privacy_ping_filter($sites);

// Display public site IDs
foreach ($public_sites as $site) {
    echo "Public site ID: " . $site->blog_id . "<br />";
}

Check if a single site is public

This example checks if a specific site is public.

$site_id = 2;
$site = get_site($site_id);

if (privacy_ping_filter(array($site))) {
    echo "The site is public.";
} else {
    echo "The site is not public.";
}

Filter sites using the ‘get_sites’ function

This example filters sites using the ‘get_sites’ function and the ‘privacy_ping_filter’ function.

$args = array(
    'number' => 10,
    'orderby' => 'registered'
);

$sites = get_sites($args);
$public_sites = privacy_ping_filter($sites);

foreach ($public_sites as $site) {
    echo "Public site: " . $site->blogname . "<br />";
}

Filter sites by custom attribute and public status

This example filters sites by a custom attribute and their public status.

// Custom function to filter sites by a custom attribute
function filter_sites_by_custom_attribute($sites) {
    $filtered_sites = array();

    foreach ($sites as $site) {
        if ($site->custom_attribute === "example_value") {
            $filtered_sites[] = $site;
        }
    }

    return $filtered_sites;
}

$sites = get_sites();
$filtered_sites = filter_sites_by_custom_attribute($sites);
$public_filtered_sites = privacy_ping_filter($filtered_sites);

foreach ($public_filtered_sites as $site) {
    echo "Public site with custom attribute: " . $site->blogname . "<br />";
}

Display public sites in a dropdown menu

This example displays public sites in a dropdown menu using the ‘wp_dropdown_sites’ function and the ‘privacy_ping_filter’ function.

$args = array(
    'id' => 'public_sites',
    'name' => 'public_sites',
    'selected' => 0
);

$sites = get_sites();
$public_sites = privacy_ping_filter($sites);

// Custom function to build the dropdown menu
function build_public_sites_dropdown($public_sites, $args) {
    $output = "<select id='{$args['id']}' name='{$args['name']}'>";
    $output .= "<option value=''>Select a public site</option>";

    foreach ($public_sites as $site) {
        $selected = $args['selected'] == $site->blog_id ? " selected='selected'" : '';
        $output .= "<option value='{$site->blog_id}'{$selected}>{$site->