In this article you’ll learn how to create custom email notification when new comment is posted in WordPress. By default you can set only the administrator to be notified via email when Anyone posts a comment or A comment is held for moderation. This can be set in Settings -> Discussion -> Email me whenever.
How to Send custom email when new comment is posted
To send your custom email to whoever you want copy and paste the following code in your child theme’s functions.php file. You see on line 2 we’re checking if the comment is approved and published. Only then we send out the email. Remember to change the $msg, $subject and $headers variables with your content.
function send_email_to_commenter_autoresponder( $comment_ID, $comment_approved ) {
if( 1 === $comment_approved ){
// Retrieve the email of the author of the current comment.
$comment_author_email = get_comment_author_email( $comment_ID );
// get post id of the comment
$comment = get_comment( $comment_ID );
$postid = $comment->comment_post_ID;
// the subject of the email
$subject = 'Your notification subject';
// the message
$msg = 'New comment on <a href="' . get_permalink( $postid ) . '">' . get_the_title( $postid ) . '</a> by' . $comment_author_email;
// the headers of the email
$headers = 'From: <your-email@example.com>' . "\r\n" . 'Reply-To: your-email@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($comment_author_email,$subject,$msg,$headers);
}
}
add_action( 'comment_post', 'send_email_to_commenter_autoresponder', 10, 2 );
The hook we used comment_post is fired immediately after a comment is inserted into the database. So after the comment is in the database, we check if it is approved and then we send out the email.
Use cases
- to send custom notification to email different than the administrator;
- you can send the commenter custom email/notification regarding his comment;
- as autoresponder for new comments;
- to thank the commenter;
- what else? Share in the comments section below.
Send email when new comment is posted in WordPress
In this article you’ll learn how to create custom email notification when new comment is posted in WordPress. By default you can set only the administrator to be notified via email when Anyone posts a comment or A comment is held for moderation. This can be set in Settings -> Discussion -> Email me whenever.
How to Send custom email when new comment is posted
To send your custom email to whoever you want copy and paste the following code in your child theme’s functions.php file. You see on line 2 we’re checking if the comment is approved and published. Only then we send out the email. Remember to change the $msg, $subject and $headers variables with your content.
The hook we used comment_post is fired immediately after a comment is inserted into the database. So after the comment is in the database, we check if it is approved and then we send out the email.
Use cases
Recent Articles
Related Articles
If you enjoyed reading this, then please explore our other articles below:
Maximizing Sales with Eye-Catching Banner Images in WooCommerce
December 20th, 2024|eCommerce, WooCommerce|
Boost Your WooCommerce Store with Automated Two-Way Up-Sell Links
May 7th, 2024|eCommerce, WooCommerce|
The Role of UX Principles in Crafting a Winning WooCommerce Store
March 18th, 2024|WooCommerce, WordPress|
Add class to menu item if product category is empty
March 15th, 2024|WooCommerce, WordPress|
Exploring WooCommerce Alternatives for WordPress: A Comprehensive Comparison
February 27th, 2024|Plugins, WooCommerce, WordPress|
Maximizing Sales: The Best Free Abandoned Cart Recovery Plugins and Email Services for Your eCommerce Business
February 26th, 2024|eCommerce, Marketing, Plugins, WooCommerce, WordPress|
5 Worst WordPress Mistakes to Avoid During Development
What is Headless WordPress
Understanding Cron Jobs: Definitions and Examples
WordPress Security: Best Practices for Protecting Your Website in 2024
A Comprehensive Guide to WordPress Multilingual Websites
The Role of UX Principles in Crafting a Winning WooCommerce Store
Add class to menu item if product category is empty
Understanding Different VPN Protocols: A Comprehensive Overview
More Articles
If you enjoyed reading this, then please explore our other articles below:
Maximizing Sales with Eye-Catching Banner Images in WooCommerce
December 20th, 2024|
Vector vs. Raster Graphics: Understanding the Difference
December 17th, 2024|
5 Worst WordPress Mistakes to Avoid During Development
December 12th, 2024|
The Future of Mobile App Design: AI and Personalization Trends
December 4th, 2024|
Shopify Polaris: A Blueprint for the Future of eCommerce Design and Development
December 3rd, 2024|
Energize Your Internal Communication Approach with Established Techniques
July 24th, 2024|
Boost Your WooCommerce Store with Automated Two-Way Up-Sell Links
May 7th, 2024|
What is Headless WordPress
April 17th, 2024|
Understanding Cron Jobs: Definitions and Examples
April 10th, 2024|
Maximizing Sales with Eye-Catching Banner Images in WooCommerce
December 20th, 2024|eCommerce, WooCommerce|
Boost Your WooCommerce Store with Automated Two-Way Up-Sell Links
May 7th, 2024|eCommerce, WooCommerce|
The Role of UX Principles in Crafting a Winning WooCommerce Store
March 18th, 2024|WooCommerce, WordPress|
Add class to menu item if product category is empty
March 15th, 2024|WooCommerce, WordPress|
Exploring WooCommerce Alternatives for WordPress: A Comprehensive Comparison
February 27th, 2024|Plugins, WooCommerce, WordPress|
Maximizing Sales: The Best Free Abandoned Cart Recovery Plugins and Email Services for Your eCommerce Business
February 26th, 2024|eCommerce, Marketing, Plugins, WooCommerce, WordPress|
Maximizing Sales with Eye-Catching Banner Images in WooCommerce
December 20th, 2024|eCommerce, WooCommerce|
Boost Your WooCommerce Store with Automated Two-Way Up-Sell Links
May 7th, 2024|eCommerce, WooCommerce|
The Role of UX Principles in Crafting a Winning WooCommerce Store
March 18th, 2024|WooCommerce, WordPress|
Add class to menu item if product category is empty
March 15th, 2024|WooCommerce, WordPress|
Exploring WooCommerce Alternatives for WordPress: A Comprehensive Comparison
February 27th, 2024|Plugins, WooCommerce, WordPress|
Maximizing Sales: The Best Free Abandoned Cart Recovery Plugins and Email Services for Your eCommerce Business
February 26th, 2024|eCommerce, Marketing, Plugins, WooCommerce, WordPress|