Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




get_active_blog_for_user [ WordPress Function ]

get_active_blog_for_user ( $user_id )
Parameters:
  • (int) $user_id The unique ID of the user
Uses:
Returns:
  • (object) The blog object
Defined at:



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.

Source


<?php
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 == || $primary->spam == || $primary->deleted == ) ) {
        
$blogs get_blogs_of_user$user_idtrue ); // if a user's primary blog is shut down, check their other blogs.
        
$ret false;
        if ( 
is_array$blogs ) && count$blogs ) > ) {
            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 == && $details->spam == && $details->deleted == ) {
                    
$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;
    }
}
?>

Examples [ wp-snippets.com ]

Top Google Search Results

More ...

User discussions [ wordpress.org ]

0 User Note(s)

None yet. Be the first!

Add New ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics