Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2012 12:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tomjn/3911427 to your computer and use it in GitHub Desktop.
WP - Notify via Email on creation of Attachment
Plugin Name: Notify on Attachment Creation
Plugin URI:
Description: Send a notification when an attachment is created
Version: 1.04
Author: Tom J Nowell
Author URI:
function new_attachment_email($att_id){
$recipients = array();
// who should it be sent to?
// send to admins of the current blog/site
$args = array();
$args[0] = 'user_email';
$wp_user_search = new WP_User_Query( array( 'role' => 'administrator','fields' => $args ) );
$admins = $wp_user_search->get_results();
foreach($admins as $admin){
$userdata = get_userdata($admin->user_id);
$recipients[] = $userdata->user_email;
// send to superadmins if multisite
$superadmins = get_super_admins();
foreach($superadmins as $superadmin){
$u = get_userdatabylogin($superadmin);
if($u != false){
$recipients[] = $u->user_email;
// send the email
// there is nobody to send this to? Abort!
$admin_email = get_option('admin_email');
$headers= "From:$admin_email\r\n";
$headers .= "Reply-To:$admin_email\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
$headers .= "content-type: text/html";
$subject = 'New Attachment uploaded';
$template = '<p>new attachment uploaded to WordPress with the ID: '.$att_id.'.</p>'. wp_get_attachment_link( $att_id,'',true,false,'click here to view this attachment' );
foreach($recipients as $to){
@wp_mail($to, $subject, $template, $headers);
Copy link

tomjn commented Oct 18, 2012

hmmm, must do some cleanup in a second revision rather than a blind copy paste, all working though

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