Skip to content

Instantly share code, notes, and snippets.

@thesnippetdev
Created September 18, 2020 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesnippetdev/d0ad79959fabcabc72f9fb2c0270136c to your computer and use it in GitHub Desktop.
Save thesnippetdev/d0ad79959fabcabc72f9fb2c0270136c to your computer and use it in GitHub Desktop.
Email Notification for Comments
<?php // remove before copy / paste
add_action('wp_insert_comment', 'email_notifications_for_comments', 99, 2);
function email_notifications_for_comments($comment_id, $comment_object) {
if ( ( $comment_object->comment_approved == 1 ) && ($comment_object->comment_parent > 0 ) ) {
$comment_parent = get_comment($comment_object->comment_parent);
$subject = '['.get_bloginfo().'] '.__('Comment Notification');
$body = sprintf(__('Hi %s,<br><br>%s YOUR TEXT HERE &#x2794;<br>%s<br><br>
The comment: <br>%s<br><br>
Reply by clicking on this link &#x2794;<br>%s'), $comment_parent->comment_author, $comment_object->comment_author, '<a href="'.get_permalink($comment_parent->comment_post_ID).'">'.get_the_title($comment_parent->comment_post_ID).'</a>', $comment_object->comment_content, '<a href="'.get_comment_link($comment_object->comment_ID).'">'.get_comment_link($comment_object->comment_ID).'</a>');
$headers[] = 'From: '.get_bloginfo().' <'.get_option('admin_email').'>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
wp_mail( $comment_parent->comment_author.' <'.$comment_parent->comment_author_email.'>', $subject, $body, $headers );
}
}
@atomGit
Copy link

atomGit commented Sep 18, 2020

thanks for sharing this - i got here via your link in the ClassicPress petition

here's something that might go well with your function - this is not my code, but i did modify it a wee bit...

/*
* COMMENTS - add 'Reply to <name>' button
*/

// https://raam.org/2013/personalizing-the-wordpress-comment-reply-link/
if (! function_exists('add_comment_author_to_reply_link')) {
    function add_comment_author_to_reply_link($link, $args, $comment){
        $comment = get_comment($comment);
        // If no comment author is blank, use 'Anonymous'
        if (empty($comment->comment_author)) {
            if (!empty($comment->user_id)){
                $user=get_userdata($comment->user_id);
                $author=$user->user_login;
            } else {
                $author = __('Anonymous');
            }
        } else {
            $author = $comment->comment_author;
        }
        // If the user provided more than a first name, use only first name
        //if(strpos($author, ' ')){
        //    $author = substr($author, 0, strpos($author, ' '));
        //}
        // Replace Reply Link with "Reply to &lt;Author First Name>"
        $reply_link_text = $args['reply_text'];
        $link = str_replace($reply_link_text, "Reply to '" . $author . "'", $link);
        return $link;
    }
    add_filter('comment_reply_link', 'add_comment_author_to_reply_link', 10, 3);
}

@WooFunctions
Copy link

That's a nice start and very much useful in terms of changing the frontend UI of the WP comment system. Just one small fix that's needed though as the text, after having clicked on "Reply to ABC", is changed to "Reply to ABC to ABC". Other than that, all good!

@atomGit
Copy link

atomGit commented Dec 18, 2020

hmmm... in my case it doesn't do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment