当媒体上载到媒体库时,我可以收到电子邮件通知吗?

时间:2012-03-21 作者:GhostToast

我有一个前端上传器,仅用于会员,使用以下代码:

<?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);
}
}?>
理想情况下,我希望这里有拖放WordPress文件上传程序,或者至少有一个进度指示器。但我离题了(不过,如果你能引导我这样做的话,我会得到一些好处)——这将是一个客户为我们上传文件的领域。我需要一封电子邮件,以某种方式与此一起生成,给管理员。有什么想法吗?

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

是的,是的,你可以

您将需要连接到add_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.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);
创建新附件时,这将向wordpress站点/网络的管理员和超级管理员发送电子邮件。

结束

相关推荐

对Single.php上的自定义查询进行分页

我正在建立一个网站,大多数页面的基本结构如下:<!-- content query --> <!-- recent posts preview query (paginated) --> <!-- related posts preview query (paginated) --> 分页的内容使用jQuery加载到同一页面上,因此当用户导航到下一页或某个节时,只需更新特定节即可。我的大多数页面实际上都是带有自定义模板的页面,我在这些模板上为每