The is_blog_user() WordPress PHP function checks if the current user belongs to a given site.
Usage
is_blog_user($blog_id);
Input:
$blog_id(int): The ID of the site you want to check the user’s membership.
Output:
- (bool): Returns
trueif the user belongs to the specified site,falseotherwise.
Parameters
$blog_id(int): The ID of the site you want to check the user’s membership.
More information
See WordPress Developer Resources: is_blog_user()
Examples
Check if current user belongs to site with ID 2
This example checks if the current user belongs to the site with an ID of 2.
if (is_blog_user(2)) {
echo 'The current user belongs to site with ID 2.';
} else {
echo 'The current user does not belong to site with ID 2.';
}
Display different content based on user membership
This example displays different content based on whether the current user belongs to a specific site or not.
$blog_id = 3;
if (is_blog_user($blog_id)) {
echo 'Welcome, member of site with ID 3!';
} else {
echo 'You are not a member of site with ID 3.';
}
Check user membership in multiple sites
This example checks if the current user belongs to any of the specified sites (with IDs 4, 5, and 6).
$sites = [4, 5, 6];
$belongs_to_site = false;
foreach ($sites as $site_id) {
if (is_blog_user($site_id)) {
$belongs_to_site = true;
break;
}
}
if ($belongs_to_site) {
echo 'The current user belongs to at least one of the specified sites.';
} else {
echo 'The current user does not belong to any of the specified sites.';
}
Create a list of sites the current user belongs to
This example creates a list of sites the current user belongs to, based on a given array of site IDs.
$sites = [1, 2, 3, 4, 5];
$user_sites = [];
foreach ($sites as $site_id) {
if (is_blog_user($site_id)) {
$user_sites[] = $site_id;
}
}
echo 'The current user belongs to the following sites: ' . implode(', ', $user_sites);
Display a message if the user belongs to a specific site
This example displays a message if the current user belongs to the site with an ID of 7.
$blog_id = 7;
if (is_blog_user($blog_id)) {
echo 'Thank you for being a member of site with ID 7!';
}