Send mail when a new Post is published in WordPress

webroomtech.com
Dec 13, 2019

--

Sending a mail when a new Post or Custom Post Type is published to your WordPress website is easy. We will use the publish_post WordPress action. Let’s see the actual code example.

PHP Snippet: Send mail in WordPress when a new Post is published

function webroom_send_mail_on_new_post( $post_id, $post ) {
if ( strpos($_SERVER[‘HTTP_REFERER’], ‘edit-question’) !== false ) {
// your action or send mail goes here if the post is edited
} else {
// send mail if the post is just published
$headers = ‘From: “Your Site <mail@example.com>’ . “\r\n” . ‘Reply-To: mail@example.com’ . “\r\n” . ‘X-Mailer: PHP/’ . phpversion();
$headers .= “Content-Transfer-Encoding: 8bit\n”;
$headers .= “Content-Type: text/html; charset=UTF-8\n”;
$headers .= ‘MIME-Version: 1.0’ . “\r\n”;

$to = ‘my.mail@example.com’;
$subject = ‘New Post Published’;
$post_title = $post->post_title;
$message = ‘Hi, new post is published on your website: ‘ . $post_title;

wp_mail($to, $subject, $message, $headers);

}
}
add_action( ‘publish_post’, ‘webroom_send_mail_on_new_post’, 10, 3 );

Paste this code snippet in your child theme’s functions.php file. Remember to change your email variables. Now every time a new post is published you’ll get notified by email.

Want to know how to send mail in WordPress when a new Custom Post Type is published?

Read the rest of the article here at Webroom.tech

--

--

webroomtech.com
webroomtech.com

Written by webroomtech.com

0 Followers

webroom.tech is created and written with one purpose: to help anyone developing their awesome WordPress and WooCommerce website.

No responses yet