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: <[email protected]>' . "\r\n" . 'Reply-To: [email protected]' . "\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.
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: <[email protected]>' . "\r\n" . 'Reply-To: [email protected]' . "\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
Recent Articles
Related Articles
If you enjoyed reading this, then please explore our other articles below:
How WooCommerce 10.5 Caches Product Objects to Cut Hydration Overhead
April 3rd, 2026|WooCommerce|
WooCommerce HPOS Full-Text Search Indexes: From Experimental Toggle to Production-Ready Order Lookup
April 3rd, 2026|WooCommerce|
How WooCommerce MCP Turns Your Store Into an AI-Accessible API
April 3rd, 2026|WooCommerce|
Fixing WooCommerce Accessibility for the European Accessibility Act
February 7th, 2026|WooCommerce|
Fixing WooCommerce Query Lag by Optimizing HPOS Custom Queries
February 7th, 2026|WooCommerce|
Fixing Interaction to Next Paint (INP) delays in WooCommerce by optimizing JavaScript execution
February 7th, 2026|WooCommerce|
Why WordPress 7.0 Centralized AI Keys Through the Connectors API
WordPress 7.0 Armstrong: Native AI Client, Abilities API, and What Got Cut
Cloudflare EmDash: A TypeScript CMS That Aims to Replace WordPress Plugin Architecture
WordPress Site Hacked? A Full Recovery Playbook From Containment to Hardening
Git From Scratch: Concepts, Commands, and the Workflow That Actually Sticks
MTA-STS in Enforce Mode: How a Single Misconfigured Policy Blocks Every Gmail Message
WordPress 7.0 RC1 Drops Real-Time Collaboration, AI Connectors, and a Redesigned Dashboard
bfcache, Speculation Rules and AI Debugging – Three Browser Features That Kill Page Load Delays
More Articles
If you enjoyed reading this, then please explore our other articles below:
How Do You Plan and Scaffold a Full-Stack Project Using GitHub Copilot and AI Tools?
September 14th, 2026|
How Do You Inherit an Existing Frontend Codebase and Extend It Into a Complete Full-Stack App With AI Tools?
September 14th, 2026|
What Are Best Practices for Managing GitHub Copilot Usage, Premium Requests, and Model Selection in VS Code?
September 13th, 2026|
How Can You Build a REST API With FastAPI, a SQLite Database, and AI Chat Features Using an Agentic Development Workflow?
September 13th, 2026|
How Do You Write an Effective agents.md Prompt to Guide GitHub Copilot Through Building a Full-Stack Application?
September 12th, 2026|
What’s the Best Way to Structure a FastAPI Backend With a React/Next.js Frontend in a Single Repository?
September 12th, 2026|
How Do You Set Up a Full-Stack Project Using GitHub Copilot as Your AI Coding Agent?
September 11th, 2026|
Why Is Docker an Essential Tool for Agentic Engineering and Autonomous Coding Workflows?
September 11th, 2026|
How Do You Install and Set Up Docker Desktop for Your First Vibe Coding Project?
September 10th, 2026|
How WooCommerce 10.5 Caches Product Objects to Cut Hydration Overhead
April 3rd, 2026|WooCommerce|
WooCommerce HPOS Full-Text Search Indexes: From Experimental Toggle to Production-Ready Order Lookup
April 3rd, 2026|WooCommerce|
How WooCommerce MCP Turns Your Store Into an AI-Accessible API
April 3rd, 2026|WooCommerce|
Fixing WooCommerce Accessibility for the European Accessibility Act
February 7th, 2026|WooCommerce|
Fixing WooCommerce Query Lag by Optimizing HPOS Custom Queries
February 7th, 2026|WooCommerce|
Fixing Interaction to Next Paint (INP) delays in WooCommerce by optimizing JavaScript execution
February 7th, 2026|WooCommerce|
How WooCommerce 10.5 Caches Product Objects to Cut Hydration Overhead
April 3rd, 2026|WooCommerce|
WooCommerce HPOS Full-Text Search Indexes: From Experimental Toggle to Production-Ready Order Lookup
April 3rd, 2026|WooCommerce|
How WooCommerce MCP Turns Your Store Into an AI-Accessible API
April 3rd, 2026|WooCommerce|
Fixing WooCommerce Accessibility for the European Accessibility Act
February 7th, 2026|WooCommerce|
Fixing WooCommerce Query Lag by Optimizing HPOS Custom Queries
February 7th, 2026|WooCommerce|
Fixing Interaction to Next Paint (INP) delays in WooCommerce by optimizing JavaScript execution
February 7th, 2026|WooCommerce|