Using WordPress ‘ms_user_list_site_class’ PHP filter

The ms_user_list_site_class filter allows you to modify the span class for a site listing on the multisite user list table in WordPress.

Usage

add_filter('ms_user_list_site_class', 'your_custom_function', 10, 4);

function your_custom_function($site_classes, $site_id, $network_id, $user) {
    // your custom code here
    return $site_classes;
}

Parameters

  • $site_classes (string[]): Array of class names used within the span tag. Default is “site-#” with the site’s network ID.
  • $site_id (int): Site ID.
  • $network_id (int): Network ID.
  • $user (WP_User): WP_User object.

More information

See WordPress Developer Resources: https://developer.wordpress.org/reference/hooks/ms_user_list_site_class/

Examples

Add Custom Class to Site Listing

Add a custom class “custom-site” to the site listing in the multisite user list table.

add_filter('ms_user_list_site_class', 'add_custom_site_class', 10, 4);

function add_custom_site_class($site_classes, $site_id, $network_id, $user) {
    $site_classes[] = 'custom-site';
    return $site_classes;
}

Highlight Administrator Sites

Highlight sites where the user has an administrator role by adding the class “admin-site”.

add_filter('ms_user_list_site_class', 'highlight_admin_sites', 10, 4);

function highlight_admin_sites($site_classes, $site_id, $network_id, $user) {
    if (in_array('administrator', $user->roles)) {
        $site_classes[] = 'admin-site';
    }
    return $site_classes;
}

Add Site ID as Class

Add the site ID as a class to the site listing in the multisite user list table.

add_filter('ms_user_list_site_class', 'add_site_id_class', 10, 4);

function add_site_id_class($site_classes, $site_id, $network_id, $user) {
    $site_classes[] = 'site-id-' . $site_id;
    return $site_classes;
}

Remove Default Site Class

Remove the default site class “site-#” from the site listing in the multisite user list table.

add_filter('ms_user_list_site_class', 'remove_default_site_class', 10, 4);

function remove_default_site_class($site_classes, $site_id, $network_id, $user) {
    $site_classes = array_diff($site_classes, ["site-{$network_id}"]);
    return $site_classes;
}

Add Network ID as Class

Add the network ID as a class to the site listing in the multisite user list table.

add_filter('ms_user_list_site_class', 'add_network_id_class', 10, 4);
function add_network_id_class($site_classes, $site_id, $network_id, $user) {
    $site_classes[] = 'network-id-' . $network_id;
    return $site_classes;
}