• Ray

    (@delveart1828)


    I have been trying for days to get wp comments to notify a custom email when a comment is made. I finally got it to work when I type the email address into the wp_mail function. like so.

    add_filter("comment_post", "email_project_designer"); 
    
    function email_project_designer(){
          wp_mail( '[email protected]' , 'subject', 'message' );
    }

    That works great. But I really need the email address to be populated using a custom meta value from the post that is receiving the new comment. The code below is as far as I got, with no luck. My feeling is the action has no idea what post to associate the function with.

    If it makes a difference, This function is being included with a plugin that uses custom post types. The meta value comes from the custom post type included with the plugin.

    add_filter("comment_post", "email_project_designer"); 
    
    function email_project_designer(){
          $p_designer_email = get_post_meta( $post->id, 'project_designer_email', true );
          wp_mail( $p_designer_email , 'subject', 'message' );
    }

    Thanks in advance for any help.

    Ray

Viewing 2 replies - 1 through 2 (of 2 total)
  • Im facing a similar problem, did you solve this?

    I am having troubles with this hook too… but with your case in theory you get it from the variable passed through the hook.

    In line 2095 of /wp-includes/comment.php is the do_action you are hooking in to.

    It says:
    do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] ); so you can see what the variable is that you can use. In this case $comment_ID

    function email_project_designer($comment_ID) {
    $comment = get_comment($comment_ID);
    $postID = $comment->comment_post_ID;

    Then just use get_post_meta($postID , 'email_key' , true); to get your email value.

    The trouble is when using this particular hook I actually cannot get it to trigger at all when a comment is added.

    Does it actually email the [email protected] example you give when someone leaves a comment??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Send comment notification to custom email from a meta value’ is closed to new replies.