get_active_blog_for_user

The timeline below displays how wordpress function get_active_blog_for_user has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Gets one of a user's active blogs.
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU (3.0.0)
 *
 * @param int $user_id The unique ID of the user
 * @return WP_Site|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[get_current_blog_id()];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_site($first_blog->userblog_id);
        } else {
            $primary = get_site($primary_blog);
        }
    } else {
        // TODO: Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        $result = add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        if (!is_wp_error($result)) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = $first_blog;
        }
    }
    if (!is_object($primary) || (1 == $primary->archived || 1 == $primary->spam || 1 == $primary->deleted)) {
        $blogs = get_blogs_of_user($user_id, true);
        // If a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if (get_current_network_id() != $blog->site_id) {
                    continue;
                }
                $details = get_site($blog_id);
                if (is_object($details) && 0 == $details->archived && 0 == $details->spam && 0 == $details->deleted) {
                    $ret = $details;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $details->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 5.4

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU (3.0.0)
 *
 * @param int $user_id The unique ID of the user
 * @return WP_Site|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[get_current_blog_id()];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_site($first_blog->userblog_id);
        } else {
            $primary = get_site($primary_blog);
        }
    } else {
        // TODO: Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        $result = add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        if (!is_wp_error($result)) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = $first_blog;
        }
    }
    if (!is_object($primary) || (1 == $primary->archived || 1 == $primary->spam || 1 == $primary->deleted)) {
        $blogs = get_blogs_of_user($user_id, true);
        // If a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if (get_current_network_id() != $blog->site_id) {
                    continue;
                }
                $details = get_site($blog_id);
                if (is_object($details) && 0 == $details->archived && 0 == $details->spam && 0 == $details->deleted) {
                    $ret = $details;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $details->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 5.1

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU (3.0.0)
 *
 * @param int $user_id The unique ID of the user
 * @return WP_Site|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[get_current_blog_id()];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_site($first_blog->userblog_id);
        } else {
            $primary = get_site($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        $result = add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        if (!is_wp_error($result)) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = $first_blog;
        }
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != get_current_network_id()) {
                    continue;
                }
                $details = get_site($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $details;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $details->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 4.9

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU (3.0.0)
 *
 * @param int $user_id The unique ID of the user
 * @return WP_Site|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[get_current_blog_id()];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_site($first_blog->userblog_id);
        } else {
            $primary = get_site($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        $result = add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        if (!is_wp_error($result)) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = $first_blog;
        }
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != get_current_network_id()) {
                    continue;
                }
                $details = get_site($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 4.7

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU 1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id The unique ID of the user
 * @return WP_Site|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    global $wpdb;
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[$wpdb->blogid];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_site($first_blog->userblog_id);
        } else {
            $primary = get_site($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
        $primary = $first_blog;
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != $wpdb->siteid) {
                    continue;
                }
                $details = get_site($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 4.5

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU 1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id The unique ID of the user
 * @return WP_Site|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    global $wpdb;
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[$wpdb->blogid];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_blog_details($first_blog->userblog_id);
        } else {
            $primary = get_blog_details($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
        $primary = $first_blog;
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != $wpdb->siteid) {
                    continue;
                }
                $details = get_blog_details($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 4.4

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU 1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id The unique ID of the user
 * @return object|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    global $wpdb;
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[$wpdb->blogid];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_blog_details($first_blog->userblog_id);
        } else {
            $primary = get_blog_details($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
        $primary = $first_blog;
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != $wpdb->siteid) {
                    continue;
                }
                $details = get_blog_details($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 4.3

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU 1.0
 *
 * @global wpdb $wpdb
 *
 * @param int $user_id The unique ID of the user
 * @return object|void The blog object
 */
function get_active_blog_for_user($user_id)
{
    global $wpdb;
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return;
    }
    if (!is_multisite()) {
        return $blogs[$wpdb->blogid];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_blog_details($first_blog->userblog_id);
        } else {
            $primary = get_blog_details($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
        $primary = $first_blog;
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != $wpdb->siteid) {
                    continue;
                }
                $details = get_blog_details($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 4.1

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU 1.0
 *
 * @param int $user_id The unique ID of the user
 * @return object The blog object
 */
function get_active_blog_for_user($user_id)
{
    global $wpdb;
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return null;
    }
    if (!is_multisite()) {
        return $blogs[$wpdb->blogid];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_blog_details($first_blog->userblog_id);
        } else {
            $primary = get_blog_details($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
        $primary = $first_blog;
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != $wpdb->siteid) {
                    continue;
                }
                $details = get_blog_details($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return null;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 3.8

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if they have one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU 1.0
 * @uses get_blogs_of_user()
 * @uses add_user_to_blog()
 * @uses get_blog_details()
 *
 * @param int $user_id The unique ID of the user
 * @return object The blog object
 */
function get_active_blog_for_user($user_id)
{
    global $wpdb;
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return null;
    }
    if (!is_multisite()) {
        return $blogs[$wpdb->blogid];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_blog_details($first_blog->userblog_id);
        } else {
            $primary = get_blog_details($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
        $primary = $first_blog;
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != $wpdb->siteid) {
                    continue;
                }
                $details = get_blog_details($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return null;
        }
        return $ret;
    } else {
        return $primary;
    }
}

WordPress Version: 3.7

/**
 * Get one of a user's active blogs
 *
 * Returns the user's primary blog, if she has one and
 * it is active. If it's inactive, function returns another
 * active blog of the user. If none are found, the user
 * is added as a Subscriber to the Dashboard Blog and that blog
 * is returned.
 *
 * @since MU 1.0
 * @uses get_blogs_of_user()
 * @uses add_user_to_blog()
 * @uses get_blog_details()
 *
 * @param int $user_id The unique ID of the user
 * @return object The blog object
 */
function get_active_blog_for_user($user_id)
{
    global $wpdb;
    $blogs = get_blogs_of_user($user_id);
    if (empty($blogs)) {
        return null;
    }
    if (!is_multisite()) {
        return $blogs[$wpdb->blogid];
    }
    $primary_blog = get_user_meta($user_id, 'primary_blog', true);
    $first_blog = current($blogs);
    if (false !== $primary_blog) {
        if (!isset($blogs[$primary_blog])) {
            update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
            $primary = get_blog_details($first_blog->userblog_id);
        } else {
            $primary = get_blog_details($primary_blog);
        }
    } else {
        //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
        add_user_to_blog($first_blog->userblog_id, $user_id, 'subscriber');
        update_user_meta($user_id, 'primary_blog', $first_blog->userblog_id);
        $primary = $first_blog;
    }
    if (!is_object($primary) || ($primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1)) {
        $blogs = get_blogs_of_user($user_id, true);
        // if a user's primary blog is shut down, check their other blogs.
        $ret = false;
        if (is_array($blogs) && count($blogs) > 0) {
            foreach ((array) $blogs as $blog_id => $blog) {
                if ($blog->site_id != $wpdb->siteid) {
                    continue;
                }
                $details = get_blog_details($blog_id);
                if (is_object($details) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0) {
                    $ret = $blog;
                    if (get_user_meta($user_id, 'primary_blog', true) != $blog_id) {
                        update_user_meta($user_id, 'primary_blog', $blog_id);
                    }
                    if (!get_user_meta($user_id, 'source_domain', true)) {
                        update_user_meta($user_id, 'source_domain', $blog->domain);
                    }
                    break;
                }
            }
        } else {
            return null;
        }
        return $ret;
    } else {
        return $primary;
    }
}