0

I have a front end uploader for members only, utilizing this code:

<?php
function insert_attachment_form($postID) {
?>
<form id="file-form" name="file-form" method="POST" action="" enctype="multipart/form-data" >
    <input type="file" id="async-upload" name="async-upload" />
    <input type="hidden" name="postID" value="<?php echo $postID; ?>" />
    <?php wp_nonce_field('client-file-upload', 'client-file-upload'); ?>
    <input type="submit" value="Upload" id="submit" name="submit" />
</form>
<?php } 

function process_attachment() {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['client-file-upload'], 'client-file-upload') ) {
    return $post->ID;
}

// Is the user allowed to edit the post or page?
if ( !current_user_can( 'publish_posts', $post->ID ))
    return $post->ID;

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_FILES )) {
    require_once(ABSPATH . 'wp-admin/includes/admin.php');
    $id = media_handle_upload('async-upload', $_POST['postID']);
    unset($_FILES);
}
}?>

Ideally I would like to have the drag-and-drop WordPress file uploader here, or at the very least, a progress indicator. But I digress (though, bonus points if you can lead me toward that) - This is going to be an area where clients upload files for us. I need an email to be generated somehow along with this, to an admin. Any ideas?

1 Answer 1

3

Yes, yes you can

You will need to hook into the add_attachment action, though there are others that may be more appropriate. Then you grab the list of administrators and send an email.

Here is a sample plugin I created for you:

<?php
/*
Plugin Name: Notify on Attachment Creation
Plugin URI: http://www.tomjn.com
Description: Send a notification when an attachment is created
Version: 1.03
Author: Tom J Nowell
Author URI: http://www.tomjn.com
*/


function new_attachment_email($att_id){
    $recipients = array();

    // who should it be sent to?

    global $wpdb;
    $key = $wpdb->prefix."user_level";
    $admins = $wpdb->get_results($wpdb->prepare("SELECT user_id from $wpdb->usermeta AS um WHERE um.meta_key ='". $key."' AND um.meta_value = 10"));

    if(!empty($admins)){
        foreach($admins as $admin){
            $userdata = get_userdata($admin->user_id);
            $recipients[] = $userdata->user_email;
        }
    } else {
        wp_die('nicht');
    }

    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!
        wp_die('oh dear god');
        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 upload';
    $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);

This will send an email to the administrators and super admins of a wordpress site/network when a new attachment is created.

12
  • Thanks! I'm super stoked you made this. FYI there was a comma missing on line 47 (before the first 1). I'm getting Warning: in_array() [function.in-array]: Wrong datatype for second argument in (data path) on line 29. Any ideas?
    – GhostToast
    Commented Mar 21, 2012 at 16:41
  • Try now, I've added checks for filled arrays and multisite, you'll want to edit the plugin anyway to add the details for the attachment that you want emailing such as the name and type, mailing to superadmins might be overkill too
    – Tom J Nowell
    Commented Mar 21, 2012 at 16:45
  • also just noticed the missing comma on the last line
    – Tom J Nowell
    Commented Mar 21, 2012 at 16:46
  • and added the URL of the attachment to the body text of the email
    – Tom J Nowell
    Commented Mar 21, 2012 at 16:49
  • By the way, it was working before (but throwing that error). But now I am getting this on plugin page: unexpected T_CONSTANT_ENCAPSED_STRING ... on line 49
    – GhostToast
    Commented Mar 21, 2012 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.