I was trying to do this very thing on a blog, and it was driving me nuts. Here’s the code I used that finally worked:
function notify_approval_to_contributor ($comment_id){
$comment = get_comment($comment_id);
if ($comment->comment_approved == 1) {
$postdata = get_post($comment->comment_post_ID);
$author = get_userdata($postdata->post_author);
mail(
$author->user_email,
'New comment on your article "'. stripslashes($postdata->post_title),
stripslashes($comment->comment_content)
);
}
}
add_action('wp_set_comment_status', 'notify_approval_to_contributor');
add_action('edit_comment', 'notify_approval_to_contributor');
Initially, it was only working when comments were approved by clicking the checkboxes to perform multiple updates and then clicking Approve. Clicking the individual Approve links to do it “Ajax-style” wasn’t working. This was because I was only using the wp_set_comment_status
action. I needed to add in the action for edit_comment
as well. Once I was handling both actions, it worked beautifully. I hope this helps.