How to get the site administrator/s by blog id

I wanted to display the site administrator/s not super administrator by blog id. Which outputs the site admin id and avatar. How to accomplish this?

Related posts

Leave a Reply

1 comment

  1. In shortcode form:

    /**
     * Shortcode for listing all admin users of a Multisite site
     *
     * Usage: [siteadmins blog="1"]
     */
    add_shortcode('siteadmins', 'wpse_55991_site_admins');
    
    function wpse_55991_site_admins($atts, $content = null)
    {   
        $site_admins = '';
        switch_to_blog( $atts['blog'] );
        $users_query = new WP_User_Query( array( 
                    'role' => 'administrator', 
                    'orderby' => 'display_name'
                    ) );
        $results = $users_query->get_results();
        foreach($results as $user)
        {
            $site_admins .= 'ID: ' . $user->ID . '<br />';
            $site_admins .= get_avatar( $user->user_email, 150 ) . '<br /><br />';
        }
        restore_current_blog();
        return $site_admins;
    }
    
    
    /* - - - - - EXTRA CODE - - - - - - */
    
    /**
     * Shortcode for listing all admin users of all Multisite sites
     *
     * Usage: [allsiteadmins]
     */
    add_shortcode('allsiteadmins', 'wpse_55991_all_site_admins');
    
    function wpse_55991_all_site_admins($atts, $content = null)
    {   
        global $wpdb;
        $blogs = $wpdb->get_results($wpdb->prepare("
            SELECT blog_id
            FROM {$wpdb->blogs}
            WHERE site_id = '{$wpdb->siteid}'
            AND spam = '0'
            AND deleted = '0'
            AND archived = '0'
            AND mature = '0' 
            AND public = '1'
        "));
        $site_admins = '';
        foreach ($blogs as $blog) 
        {
            switch_to_blog( $blog->blog_id );
            $users_query = new WP_User_Query( array( 
                        'role' => 'administrator', 
                        'orderby' => 'display_name'
                        ) );
            $results = $users_query->get_results();
            $site_admins .= 'Blog ID: ' . $blog->blog_id . '<pre>' . print_r($results,true) . '</pre>';
        }
        restore_current_blog();
        return $site_admins;
    }