Email Alert When Super Admin Rights Change
Emails the network admin address whenever someone is granted or loses super admin rights, with who made the change.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
$wpide_alert_to = get_site_option( 'admin_email' ); // Recipient.
$wpide_alert = function ( $user_id, $granted ) use ( $wpide_alert_to ) {
$user = get_userdata( $user_id );
$actor = wp_get_current_user();
if ( ! $user || ! $wpide_alert_to ) {
return;
}
wp_mail(
$wpide_alert_to,
sprintf( '[%s] Super admin %s: %s', get_network()->site_name, $granted ? 'granted' : 'revoked', $user->user_login ),
sprintf(
"%1\$s was %2\$s super admin rights by %3\$s on %4\$s.\nIP: %5\$s\n",
$user->user_login,
$granted ? 'given' : 'removed from',
$actor->exists() ? $actor->user_login : 'a system process',
wp_date( 'Y-m-d H:i' ),
isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : 'n/a'
)
);
};
add_action( 'granted_super_admin', function ( $user_id ) use ( $wpide_alert ) {
$wpide_alert( $user_id, true );
} );
add_action( 'revoked_super_admin', function ( $user_id ) use ( $wpide_alert ) {
$wpide_alert( $user_id, false );
} );
Notes
Change $wpide_alert_to to send the alert somewhere else.
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.