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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.