Skip to content

Instantly share code, notes, and snippets.

@sirchrispy
Last active March 1, 2016 17:10
Show Gist options
  • Save sirchrispy/b912a7ee22590e543d07 to your computer and use it in GitHub Desktop.
Save sirchrispy/b912a7ee22590e543d07 to your computer and use it in GitHub Desktop.
WordPress MultiSite: Display a List of Network User Emails by Role
.emails {
margin-bottom: 40px;
}
<?php
/*
Plugin Name: Who's What?
Plugin URI: http://thecookingdish.com
Description: Displays a list of user emails by role under the 'Users' admin menu. Allows for bulk send via user's default email client.
Version: 1.1.0
Author: Chris Mower
Author URI: http://thecookingdish.com
*/
/**
* If this file is called directly, abort
*/
if ( !defined( 'ABSPATH' ) ) {
wp_die( __( "You are not allowed to access this page directly.", 'whoswhat' ) );
}
/**
* Load admin styles
*/
function whoswhat_load_admin_styles() {
wp_register_style( 'whoswhat_admin_styles', plugin_dir_url( __FILE__ ) . '/style-admin.css' );
wp_enqueue_style( 'whoswhat_admin_styles' );
}
add_action( 'admin_enqueue_scripts', 'whoswhat_load_admin_styles' );
/**
* Create a page on the Admin screen under users.php
*/
function whoswhat_admin_plugin_menu() {
add_submenu_page( 'users.php', 'Email Users by Role', 'Email Users', 'manage_network', 'email-users-by-role', 'whoswhat_display_the_emails');
}
add_action( 'admin_menu', 'whoswhat_admin_plugin_menu' ); // add to blog admin menus
add_action( 'network_admin_menu', 'whoswhat_admin_plugin_menu' ); // add to network admin menu
/**
* Loop through each blog and grab the users
*
* @author Chris Mower
* @since 1.0.0
* @param string, $user_role, A pre-defined WordPress user role
* @return array, a list of users as defined by $user_role
*/
function whoswhat_get_the_blog_users_emails( $user_role, $sendmail = false ) {
// define global variable so that we can query the DB
global $wpdb;
// get a list of all the blogs on MultiSite install
$blogs = $wpdb->get_results( "SELECT blog_id,domain,path FROM " . $wpdb->base_prefix . "blogs" );
// define initial output
$output = '';
// define users array
$users = array();
// loop through each blog on the network
foreach( $blogs as $blog ) {
// get users from each of the blogs using the blog_id
$blogusers = get_users( array(
'blog_id' => $blog->blog_id,
'role' => $user_role,
'orderby' => 'email',
) );
// loop through each of the returned users
foreach( $blogusers as $bloguser ) {
// check to see if they're not already in the users array
if( !in_array( $bloguser, $users ) ) {
// add to array based on user email
$users[$bloguser->user_email] = $bloguser;
}
}
}
// sort the users array alphabetically
ksort($users);
// loop through the users array
foreach ( $users as $user ) {
// determine if the emails are for display or bulk email
switch ( $sendmail ) {
// for bulk email
case true:
$output .= esc_html( $user->user_email ) . ',';
break;
// for display
case false:
default:
$output .= '<span><a href="mailto:' . esc_html( $user->user_email ) . '">' . esc_html( $user->user_email ) . '</a></span>, ';
break;
}
}
// return the output.
return $output;
}
/**
* Determine how to display the returned emails
*
* @author Chris Mower
* @since 1.0.0
*/
function whoswhat_display_the_emails() {
?>
<div class="wrap">
<h1>Network User Emails</h1>
<div class="emails admin-emails">
<h2>Administrator Emails</h2>
<p><?php echo whoswhat_get_the_blog_users_emails( 'administrator' ); ?></p>
<p>
<a class="button" href="mailto:<?php echo whoswhat_get_the_blog_users_emails( 'administrator', true ); ?>">Email Administrators</a>
</p>
</div>
<div class="emails editor-emails">
<h2>Editor Emails</h2>
<p><?php echo whoswhat_get_the_blog_users_emails( 'editor' ); ?></p>
<p>
<a class="button" href="mailto:<?php echo whoswhat_get_the_blog_users_emails( 'editor', true ); ?>">Email Editors</a>
</p>
</div>
<div class="emails author-emails">
<h2>Author Emails</h2>
<p><?php echo whoswhat_get_the_blog_users_emails( 'author' ); ?></p>
<p>
<a class="button" href="mailto:<?php echo whoswhat_get_the_blog_users_emails( 'author', true ); ?>">Email Authors</a>
</p>
</div>
<div class="emails contributor-emails">
<h2>Contributor Emails</h2>
<p><?php echo whoswhat_get_the_blog_users_emails( 'contributor' ); ?></p>
<p>
<a class="button" href="mailto:<?php echo whoswhat_get_the_blog_users_emails( 'contributor', true ); ?>">Email Contributors</a>
</p>
</div>
<div class="emails subscriber-emails">
<h2>Subscriber Emails</h2>
<p><?php echo whoswhat_get_the_blog_users_emails( 'subscriber' ); ?></p>
<p>
<a class="button" href="mailto:<?php echo whoswhat_get_the_blog_users_emails( 'subscriber', true ); ?>">Email Subscribers</a>
</p>
</div>
<div class="emails event-contributor-emails">
<h2>Event Contributor Emails</h2>
<p><?php echo whoswhat_get_the_blog_users_emails( 'ai1ec_event_assistant' ); ?></p>
<p>
<a class="button" href="mailto:<?php echo whoswhat_get_the_blog_users_emails( 'ai1ec_event_assistant', true ); ?>">Email Event Contributors</a>
</p>
</div>
</div>
<?php
}
@sirchrispy
Copy link
Author

Version 1.1.0

  • Added alphabetical sorting, ascending order

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment