Creating Custom Email Notifications for WordPress Comments
Basic Approach
WordPress already includes a basic comment notification system, but you can enhance it or create custom notifications using WordPress hooks and the mail function.
Method 1: Using WordPress Functions
Here's a simple function to customize the default comment notification email:
This code customizes the email content and recipients:
function custom_comment_notification($comment_ID, $comment) {
// Get the comment data
$comment = get_comment($comment_ID);
$post = get_post($comment->comment_post_ID);
// Email settings
$to = get_option('admin_email');
$subject = 'New Comment on: ' . $post->post_title;
$message = "A new comment has been posted on: " . $post->post_title . "\n\n";
$message .= "Author: " . $comment->comment_author . "\n";
$message .= "Email: " . $comment->comment_author_email . "\n";
$message .= "Comment: " . $comment->comment_content . "\n\n";
$message .= "View comment: " . get_comment_link($comment_ID);
// Send email
wp_mail($to, $subject, $message);
}
add_action('comment_post', 'custom_comment_notification', 10, 2);
Method 2: Multiple Recipients
To send notifications to multiple email addresses:
function multi_recipient_comment_notification($comment_ID, $comment) {
$recipients = array(
'admin@yoursite.com',
'editor@yoursite.com'
);
$post = get_post($comment->comment_post_ID);
$subject = 'New Comment on: ' . $post->post_title;
$message = "New comment posted\n\n";
$message .= "Post: " . $post->post_title . "\n";
$message .= "Comment: " . $comment->comment_content;
// Send to each recipient
foreach($recipients as $email) {
wp_mail($email, $subject, $message);
}
}
add_action('comment_post', 'multi_recipient_comment_notification', 10, 2);
Security Considerations
- Always sanitize email content
- Use WordPress's built-in
wp_mail()
function instead of PHP's mail()
- Validate email addresses before sending
- Consider implementing rate limiting
HTML Email Template
For a more professional looking notification:
function html_comment_notification($comment_ID, $comment) {
$to = get_option('admin_email');
$post = get_post($comment->comment_post_ID);
$subject = 'New Comment on: ' . $post->post_title;
$headers = array('Content-Type: text/html; charset=UTF-8');
$message = '<html><body>';
$message .= '<h2>New Comment Posted</h2>';
$message .= '<p><strong>Post:</strong> ' . $post->post_title . '</p>';
$message .= '<p><strong>Author:</strong> ' . $comment->comment_author . '</p>';
$message .= '<p><strong>Comment:</strong><br>' . $comment->comment_content . '</p>';
$message .= '<p><a href="' . get_comment_link($comment_ID) . '">View Comment</a></p>';
$message .= '</body></html>';
wp_mail($to, $subject, $message, $headers);
}
add_action('comment_post', 'html_comment_notification', 10, 2);
Recommended Plugins
-
Better Notifications for WP - Offers a user-friendly interface for customizing all WordPress notifications.
Plugin Link
-
Comment Notifications Extended - Adds advanced notification options specifically for comments.
Plugin Link
Common Pitfalls to Avoid
- Don't send notifications for spam comments
- Avoid sending too many emails at once
- Don't include sensitive information in emails
- Test email delivery in different environments
Best Practices
- Use conditional checks to prevent unnecessary notifications
- Implement proper error handling
- Allow users to unsubscribe from notifications
- Follow email design best practices for HTML emails
- Use proper hooks and filters for maximum compatibility
These solutions can be customized further based on your specific needs and requirements.