評論回複郵件通知是指:你回複了A的評論,A會收到相應的郵件通知。
建立一個外掛程式,只需要一個php檔案就可以了.
首先是add_action
add_action('comment_post', 'comment_notify');
然後實現我們的發送郵件函數。主要就是根據wordpress的API擷取相關資訊,然後調用wp_mail函數進行發送即可。
修改源郵件地址可以在header裡面修改:
$message_headers = 'From: Acm之家 <no-reply@acmerblog.com>' . "\r\n\\";
注意區分 comment 和 old_comment。後者指被回複的評論。
這裡以我的網站 ACM之家 www.acmerblog.com 為例:
<?phpadd_action('comment_post', 'comment_notify');function comment_notify($comment_reply_id){ $comment = get_comment($comment_reply_id); if($comment->comment_parent != 0) { $old_comment = get_comment($comment->comment_parent);//被回複的評論 if($old_comment->user_id == 0) { $email = $old_comment->comment_author_email; //A評論者的郵箱,作為目的地址 $name = $comment->comment_author; $content = $comment->comment_content; $post = get_post($comment->comment_post_ID); $title = $post->post_title; $link = get_permalink($comment->comment_post_ID); $bname=get_option('blogname'); //擷取到網站的名稱, ACM之家。 這裡沒有用這個變數,後面直接寫死了 $blogname = wp_specialchars_decode($bname, ENT_QUOTES); $subject = sprintf('[%1$s]在[%2$s]中回複了你', $name, $title ); //郵件的主題//$subject = sprintf('[%1$s] 評論回複: "%2$s"', $blogname, $title ); $notify_message = sprintf('你在《%s》的評論有新回複', $title ) . "\r\n"; $notify_message .= sprintf( '評論者 : %1$s ', $name ) . "\r\n"; $notify_message .= '評論內容: ' . "\r\n" . $content . "\r\n\r\n"; $notify_message .= '您可在這裡查看這篇文章的評論: ' . "\r\n"; $notify_message .= $link . "#comments\r\n\r\n"; $message_headers = 'From: Acm之家 <no-reply@acmerblog.com>' . "\r\n\\"; //header的應用可以更改郵件的源地址 $message_headers .= "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; wp_mail( $email, $subject, $notify_message, $message_headers ); } }}?>