Skip to content

Instantly share code, notes, and snippets.

@tomjn
Created October 18, 2012 12:15
Show Gist options
  • Save tomjn/3911427 to your computer and use it in GitHub Desktop.
Save tomjn/3911427 to your computer and use it in GitHub Desktop.
WP - Notify via Email on creation of Attachment
<?php
/*
Plugin Name: Notify on Attachment Creation
Plugin URI: http://www.tomjn.com
Description: Send a notification when an attachment is created
Version: 1.04
Author: Tom J Nowell
Author URI: http://www.tomjn.com
*/
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();
if(!empty($admins)){
foreach($admins as $admin){
$userdata = get_userdata($admin->user_id);
$recipients[] = $userdata->user_email;
}
}
// send to superadmins if multisite
if(is_multisite()){
$superadmins = get_super_admins();
if(!empty($superadmins)){
foreach($superadmins as $superadmin){
$u = get_userdatabylogin($superadmin);
if($u != false){
if(!in_array($u->user_email,$recipients)){
$recipients[] = $u->user_email;
}
}
}
}
}
// send the email
if(empty($recipients)){
// there is nobody to send this to? Abort!
return;
}
$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);
}
}
add_action('add_attachment','new_attachment_email',1,1);
@tomjn
Copy link
Author

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