Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.