Halloween Sale 15% off Ends in See the plans
PHP

List a User's Sites on Their Profile

Shows super admins every site a user belongs to, with their role on each, at the bottom of the user's profile screen.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! is_multisite() ) {
    return;
}

$wpide_user_sites = function ( $user ) {
    if ( ! is_super_admin() ) {
        return;
    }

    $blogs = get_blogs_of_user( $user->ID );
    ?>
    <h2><?php esc_html_e( 'Sites' ); ?></h2>
    <table class="widefat striped" style="max-width:720px">
        <thead><tr><th><?php esc_html_e( 'Site' ); ?></th><th><?php esc_html_e( 'Role' ); ?></th></tr></thead>
        <tbody>
        <?php if ( ! $blogs ) : ?>
            <tr><td colspan="2"><?php esc_html_e( 'This user does not belong to any site.' ); ?></td></tr>
        <?php endif; ?>
        <?php foreach ( $blogs as $blog ) :
            $caps  = get_user_meta( $user->ID, $GLOBALS['wpdb']->get_blog_prefix( $blog->userblog_id ) . 'capabilities', true );
            $roles = is_array( $caps ) ? implode( ', ', array_keys( array_filter( $caps ) ) ) : '';
            ?>
            <tr>
                <td><a href="<?php echo esc_url( get_admin_url( $blog->userblog_id ) ); ?>"><?php echo esc_html( $blog->blogname ); ?></a><br><small><?php echo esc_html( $blog->siteurl ); ?></small></td>
                <td><?php echo esc_html( $roles ? $roles : '—' ); ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php
};

add_action( 'show_user_profile', $wpide_user_sites );
add_action( 'edit_user_profile', $wpide_user_sites );

Notes

Only super admins see the list.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.