If new comment posted in custom post – send notification to custom email from custom field

I have custom post type “Art masters”.
Each post is master’s profile.

In their profiles isset custom fiels name “master_email”.
I need to send for master email notification every time if new comment is posted.

Read More

How i can call post new comment function for use wp_mail? Thank you for help!

Related posts

Leave a Reply

2 comments

  1. You can try something like this in your functions.php

    function send_comment_email_notification( $comment_ID, $commentdata ) {
        $comment = get_comment( $comment_id );
        $postid = $comment->comment_post_ID;
        $master_email = get_post_meta( $postid, 'master_email', true);
        if( isset( $master_email ) && is_email( $master_email ) ) {
            $message = 'New comment on <a href="' . get_permalink( $postid ) . '">' .  get_the_title( $postid ) . '</a>';
            add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
            wp_mail( $master_email, 'New Comment', $message );
        }
    }
    add_action( 'comment_post', 'send_comment_email_notification', 11, 2 );
    
  2. You can try this. Example:helenyhou@example.com

    add_filter('wp_mail_from','yoursite_wp_mail_from'); 
    
    function yoursite_wp_mail_from($content_type) {
       return 'helenyhou@example.com'; 
    } 
    
    add_filter('wp_mail_from_name','yoursite_wp_mail_from_name'); 
    
    function yoursite_wp_mail_from_name($name) {
       return 'Helen Hou-Sandi'; 
    }