Using WordPress ‘get_active_blog_for_user()’ PHP function

The get_active_blog_for_user() WordPress PHP function returns one of a user’s active blogs.

Usage

get_active_blog_for_user($user_id);

Example:

Input:

get_active_blog_for_user(1);

Output:
An object containing the active blog information.

Parameters

  • $user_id (int): The unique ID of the user.

More information

See WordPress Developer Resources: get_active_blog_for_user()

Examples

Display the primary blog of a user

$user_id = 1;
$blog = get_active_blog_for_user($user_id);

if ($blog) {
    echo "User's primary blog is: " . $blog->blogname;
} else {
    echo "No active blogs found for this user.";
}

Get the blog URL for a user

$user_id = 2;
$blog = get_active_blog_for_user($user_id);

if ($blog) {
    echo "User's blog URL: " . $blog->siteurl;
} else {
    echo "No active blogs found for this user.";
}

Check if a user’s blog is public

$user_id = 3;
$blog = get_active_blog_for_user($user_id);

if ($blog && $blog->public == 1) {
    echo "This user's blog is public.";
} else {
    echo "This user's blog is not public.";
}
$user_id = 4;
$blog = get_active_blog_for_user($user_id);

if ($blog) {
    echo "Active blog information for user $user_id:";
    echo "\nBlog ID: " . $blog->blog_id;
    echo "\nBlog Name: " . $blog->blogname;
    echo "\nSite URL: " . $blog->siteurl;
} else {
    echo "No active blogs found for this user.";
}

Get the blog language for a user

$user_id = 5;
$blog = get_active_blog_for_user($user_id);

if ($blog) {
    echo "User's blog language: " . $blog->language;
} else {
    echo "No active blogs found for this user.";
}

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *