notification email after media upload
-
Hi
I would like to send a notification email to admin whenever a contributor uplod a file in the media area
I tried to edit the file upload.php without results, can anyone help me?
Tks
-
For starters, do not ever edit WordPress core files. That’s the quickest way to bring your site down and/or introduce a security hole. You will effectively need to write a plugin to do this. Or you could look around and see if someone has already created one.
I searched a lot but could not find a suitable plugin
I just want to add something like this
<?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?>
at the end of the upload function, i don’t think that this code will introduce a security hole…or not?But i don’t find where to put this code…in my upload.php file
Thanks
i found this but doesn’t work
where i should put this code?add_action('admin_init', 'redirect_after_media_save'); function redirect_after_media_save() { global $pagenow; // when visiting the upload screen, we save the number of attachments if ( $pagenow == 'media-new.php' ) { $attachments_before = array_sum((array)wp_count_attachments()); update_option('count_attach_before', $attachments_before); } if ( $pagenow == 'upload.php' ) { // we are on media library page // get attachments count before and after upload $attachments_before = get_option('count_attach_before'); $attachments_after = array_sum((array)wp_count_attachments()); if ( // there are new files uploaded ( wp_get_referer() == admin_url('media-new.php') && $attachments_after > $attachments_before ) || // or we have just edited media file isset($_GET['posted']) ) { // redirect to desired location wp_mail('apelissetti@gmail.com', 'subject', 'message', $headers, $attachments ); exit; } } }
[Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]
Listen to esmi.. she knows what she is talking about!
Instead of editing core files; you will want to create a plugin (or use your child themes
functions.php
file.There is no action which can be fired during the ‘save media’ process. So, what we have to do is count total attachments BEFORE the upload process.. and compare it to total attachments AFTER the upload process. If the ‘before’ is less than the ‘after’.. we know an attachment was uploaded.
This Function (on pastebin.com) should work for you. Just place it in your child theme
functions.php
file (or make it a plugin).Thank Josh
but doesn’t work in my functions.php file
Did you define your variables in the
wp_mail()
function?For example, did you define the
$to, $subject, $message, and $headers
variables?i wrote
wp_mail( ‘apelissetti@gmail.com’, $subject, $message, $headers, $attachments );
not enough?
Nope. How is WP going to know what is inside those other variables??
Let’s try this:
$to = 'apelissetti@gmail.com'; $subject = 'New WP Attachment!'; $message = 'An attachment has been uploaded.'; $headers = 'From: My Name <myname@example.com>' . "\r\n"; wp_mail( $to, $subject, $message, $headers );
yesss
superI have to attend this forum to learn something more…
THANKS
Lol – My pleasure 😉
(Please mark your thread as ‘resolved’. You can do this over on the right side of this page.)
second lesson…
to insert the logged in user’s email….what variable should i put here?
$headers = ‘From: My Name <myname@example.com>’ . “\r\n”;
Well.. let’s try this:
$current_user = wp_get_current_user(); // Get current user object $current_user_email = $current_user->user_email; // Get current user email $current_username = $current_user->user_login; // Get current user login name $to = 'redacted email'; $subject = 'New WP Attachment!'; $message = 'An attachment has been uploaded.'; $headers = 'From: '.$current_username.' <'.$current_user_email.'>' . "\r\n"; wp_mail( $to, $subject, $message, $headers );
Here, we are defining the username and the user email from the currently logged in user. Then, we are concatenating the variables to the
$headers
variable string.Watch the syntax… but that should work for you.
Hi Josh
the script (also the old one with fixed email) works only sometimesIf i stay logged don’t come the notification email
I have to log out and re-log with other user.
Do you have some solotuons for that?
Tks
- The topic ‘notification email after media upload’ is closed to new replies.