0

Is there a way to add the comment_notification_text filter to a custom post type? I'm trying to send a custom email when a comment is left on a custom post type

I am currently using this filter:

add_filter( 'comment_notification_headers', 'set_mail_html_content_type', 10, 2 );

UPDATE WITH COMMENT_POST HOOK

function notify_postauthor_comment($comment_id) {

$comment = get_comment( $comment_id );
$post_ID = $comment->comment_post_ID;
$author_ID = get_post_field( 'post_author', $post_ID );
$user_info = get_userdata($author_ID);
$email = $user_info->user_email;
$pTitle = $comment->post_title;
$cAuthor = $comment->comment_author;
$cContent = $comment->comment_content;
$cUrl = get_comment_link( $comment_id );

$to = $email;
$subject = 'Your Post,'.$pTitle.', just received a comment!' ;
$headers[] = 'From: [email protected]';
$headers[] = 'Content-Type: text/html; charset=UTF-8';

ob_start(); 

include("template/email_header.php");
?>

<p>Congrats! You just received a new comment from <?php echo $cAuthor; ?> on your post:</p>
  <p><?php echo $pTitle; ?></p>
  <p><strong>Comment:</strong><br>
    <?php echo $cContent; ?></p>

<?php 
include("template/email_footer.php");
$message = ob_get_contents();
$message = stripslashes($message);
ob_end_clean();

//return $message;

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

}

add_action('comment_post', 'notify_postauthor_comment', 11, 2);
1
  • have you try to fire comment_notification_text with a function?
    – Jevuska
    Mar 21, 2016 at 16:09

1 Answer 1

3

Using comment_notification_text you will get comment notification text and comment ID. To check the post type you need to get the post associated with comment and then you can check for post type

Example (Read the inline comments):

function custom_notificaion($notify_message, $comment_id) {
    $comment_obj = get_comment($comment_id); //Get comment object
    $comment_post = get_post($comment_obj->comment_post_ID); //Get post object
    
    //Check if it is your post type
    if (isset($comment_post->post_type) && $comment_post->post_type == 'your-post-type') {
        return __('Custom message here', 'text-domain'); //Return the custom message here
    }
    
    return $notify_message; //Return default message for rest of the posts
}
add_filter('comment_notification_text', 'custom_notificaion', 10, 2);
3
  • thanks, the other option i found was to use the comment_post hook to work across the board
    – DEM
    Mar 21, 2016 at 16:50
  • Glad to help! I wonder how did you managed using comment_post hook. It also return the comment ID only!
    – Sumit
    Mar 21, 2016 at 16:59
  • 1
    see updated orginal question, guess I used a workaround by just sending an email via wp_mail
    – DEM
    Mar 21, 2016 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.