<?php function notify_post_status( $post_id = NULL, $user = '' ) { if ( ! isset($_POST["post_id"]) ) { $post_id = $_POST["post_id"]; } else { return $post_id; } $post = get_post($post_id); $status = $post->post_status; $type = $post->post_type; $author_id = intval($post->post_author); $user = get_userdata( $author_id ); //ignore admin if( $author_id == 1 ) return; // Which statuses should we send email for ? $send_statuses = array("publish","draft"); // Send mails only for published (approved) || draft (rejected) if( ! in_array($status,$send_statuses)) return; // Only send for a specific custom post type ? //if( $type != "my_custom_post_type" ) return $post_id; $title = $post->post_title; $content = $post->post_content; $blogname = get_bloginfo( 'name' ); // $to_admin = get_bloginfo( 'admin_email' ); $to = $user->user_email; $frommail = 'noreply@my_domain.com'; $permalink = get_permalink( $post_id ); $headers = sprintf( "From: %s <%s>\r\n", $blogname, $frommail ); $subject = sprintf( __( 'Your post status : %s','my_textdomain' ), $status ); $msg = sprintf( __( 'Your post titled: %s ','my_textdomain' ), $title ) . "\r\n"; $msg .= sprintf( __( 'status has been changed to : %s','my_textdomain' ), $title ) . "\r\n"; if($status == "publish" ) { $msg .= sprintf( __( 'You can view it here : %s' ), $permalink ) . "\r\n"; } // Save sent email for each status so it doesnt send email every time $sent_mails = get_post_meta($post_id,'post_notif_sent_mails',true); if($sent_mails) { // have sent mail for this status ? if ( in_array($status,$sent_mails) ) { return; } else { $sent_mails[] = $status; } } else { // no mail has been sent for this status, crate array, insert stat $sent_mails = array($status); } // send the mail, update the metadata update_post_meta($post_id,"post_notif_sent_mails",$sent_mails); wp_mail( $to, $subject, $msg, $headers ); } ?>