Using WordPress ‘enable_live_network_counts’ PHP filter

The enable_live_network_counts WordPress PHP filter allows you to control the updating of network site or user counts when a new site is created.

Usage

add_filter('enable_live_network_counts', 'your_custom_function', 10, 2);

function your_custom_function($small_network, $context) {
    // Your custom code here
    return $small_network;
}

Parameters

  • $small_network (bool) – Indicates whether the network is considered small.
  • $context (string) – Context, either ‘users’ or ‘sites’.

More information

See WordPress Developer Resources: enable_live_network_counts

Examples

Disable live network counts for large networks

Disable live network counts if the network has more than 1000 users or sites.

add_filter('enable_live_network_counts', 'disable_large_network_counts', 10, 2);

function disable_large_network_counts($small_network, $context) {
    $threshold = 1000;

    if ($context == 'users') {
        $count = get_user_count();
    } else {
        $count = get_blog_count();
    }

    return ($count <= $threshold);
}

Always enable live network counts

Enable live network counts regardless of the network size.

add_filter('enable_live_network_counts', 'always_enable_counts', 10, 2);

function always_enable_counts($small_network, $context) {
    return true;
}

Always disable live network counts

Disable live network counts regardless of the network size.

add_filter('enable_live_network_counts', 'always_disable_counts', 10, 2);

function always_disable_counts($small_network, $context) {
    return false;
}

Enable live network counts for small user networks only

Enable live network counts only for networks with fewer than 500 users.

add_filter('enable_live_network_counts', 'small_user_network_counts', 10, 2);

function small_user_network_counts($small_network, $context) {
    if ($context == 'users') {
        $count = get_user_count();
        return ($count < 500);
    }

    return $small_network;
}

Enable live network counts for small site networks only

Enable live network counts only for networks with fewer than 250 sites.

add_filter('enable_live_network_counts', 'small_site_network_counts', 10, 2);

function small_site_network_counts($small_network, $context) {
    if ($context == 'sites') {
        $count = get_blog_count();
        return ($count < 250);
    }

    return $small_network;
}