워드프레스 댓글 알림 이메일 변경하기

wordpress comments notification

개요

워드프레스에서 댓글을 남기거나 승인 대기 중인 경우 알림 이메일을 전송할지 여부는 워드프레스 대시보드의 설정 > 토론에서 설정할 수 있습니다.

댓글을 남기거나 승인 대기 중일 때 이메일 전송과 관련된 필터는 comment_moderation_recipients and comment_notification_recipients입니다.

필터 이름에서 알 수 있듯이 comment_moderation_recipients는 승인이 필요한 댓글이 있을 경우에 알림을 받을 수신자 이메일 ID 목록을 필터링하고 comment_notification_recipients는 댓글 알림을 받을 이메일 주소 목록을 필터링합니다. 기본적으로 댓글 알림은 글 작성자에게만 전송됩니다. 이 필터를 사용하면 다른 사용자를 추가할 수 있습니다.

댓글 알림 이메일을 받지 않도록 설정하기

댓글을 남기거나 승인 대기 중인 댓글이 있을 때 일절 알림 이메일이 전송되지 않게 하려면 다음과 같이 빈 배열(array)을 사용하면 됩니다.

function se_comment_moderation_recipients( $emails, $comment_id ) {
// 승인 대기 중 댓글/새 댓글 알림 비활성화하기
$emails = array();
return $emails;
}
add_filter( 'comment_moderation_recipients', 'se_comment_moderation_recipients', 11, 2 );
add_filter( 'comment_notification_recipients', 'se_comment_moderation_recipients', 11, 2 );
// Source: sourcexpress.com

관리자만 댓글 알림 이메일 받기

사이트 관리자에게만 댓글 알림 이메일이 전송되도록 하려면 다음과 같은 코드를 사용합니다.

function se_comment_moderation_recipients( $emails, $comment_id ) {
// 관리자에게만 댓글 알림 이메일 보내기
$emails = array( get_option( 'admin_email' ) );

return $emails;
}
add_filter( 'comment_moderation_recipients', 'se_comment_moderation_recipients', 11, 2 );
add_filter( 'comment_notification_recipients', 'se_comment_moderation_recipients', 11, 2 );

만약 멀티 사이트인 경우 get_option 대신 get_site_option을 사용하도록 합니다.

get_option()는 현재 블로그에 대한 옵션을 반환합니다. 단일 사이트 설치에서는 현재 블로그가 유일한 블로그입니다. 그러므로 get_option()은 현재 블로그에 대한 옵션을 반환합니다.

get_site_option()은 네트워크 차원에서 옵션을 검색할 때 사용됩니다. 따라서 네트워크의 임의의 사이트에서 동일한 옵션을 얻을 수 있습니다. 만약 이 함수를 단일 설치에서 사용하면 get_option()과 동일한 값을 반환합니다. get_site_option()get_option()에서 트리하지 않는 필터 후크를 트리거하므로 값이 변경될 수 있습니다.

참고로 $wpdb->options 테이블은 특정 블로그용이지만 네트워크 차원의 옵션은 $wpdb->sitemeta 테이블에 저장됩니다(참고).

글 작성자에게만 댓글 알림 이메일 전송

댓글이 달리거나 댓글 승인이 필요한 경우 글 작성자에게 알림 이메일을 보내려면 다음과 같은 코드를 사용합니다.

function se_comment_moderation_recipients( $emails, $comment_id ) {
$comment = get_comment( $comment_id );
$post = get_post( $comment->comment_post_ID );
$user = get_user_by( 'id', $post->post_author );

// 글 작성자가 수정할 권한이 있는 경우 글 작성자만을 반환합니다.
if ( user_can( $user->ID, 'edit_published_posts' ) && ! empty( $user->user_email ) ) {
$emails = array( $user->user_email );
}

return $emails;
}
add_filter( 'comment_moderation_recipients', 'se_comment_moderation_recipients', 11, 2 );
add_filter( 'comment_notification_recipients', 'se_comment_moderation_recipients', 11, 2 );

임의의 이메일 추가하기

특정 이메일 주소를 지정하려는 경우 다음과 같이 사용합니다.

function se_comment_moderation_recipients( $emails, $comment_id ) {
// 임의의 이메일 추가
$emails = array( 'exmaple@mail.com' );

return $emails;
}
add_filter( 'comment_moderation_recipients', 'se_comment_moderation_recipients', 11, 2 );
add_filter( 'comment_notification_recipients', 'se_comment_moderation_recipients', 11, 2 );

위의 코드는 사용 중인 워드프레스 테마의 함수 파일(funcitons.php)에 추가하도록 합니다. 가급적 자식 테마(차일드 테마)를 만들어서 자식 테마 내의 함수 파일에 추가하는 것이 좋습니다. 사용자 코드를 함수 파일에 추가하는 방법은 여기를 참고해보시기 바랍니다.


참고: 워드프레스에서 발송되는 이메일(제목과 본문)을 커스터마이징하고 싶은 경우 WordPress Custom Emails이라는 플러그인을 사용해볼 수 있습니다. 이 플러그인은 새 등록 이메일, 비밀번호 재설정 요청 이메일, 비밀번호 변경 시 사용자 알림, 비밀번호 변경 시 관리자 알림, 승인 대기 중인 새 댓글 알림 이메일, 새 댓글이 달릴 때 관리자에게 발송되는 이메일 등을 커스터마이징할 수 있고 이메일 설정 및 STMP가 지원됩니다. Custom Emails 플러그인은 판매 수가 별로 되지 않지만 사용자 평가는 4.50(5.00 만점)으로 비교적 괜찮은 편입니다.


댓글 남기기

* 이메일 주소는 공개되지 않습니다.