wp_notify_postauthor

Notify an author (and/or others) of a comment/trackback/pingback on a post.

Description

wp_notify_postauthor( (int|WP_Comment) $comment_id, (null) $deprecated = null ); 

Parameters (2)

0. $comment_id (int|WP_Comment)
Comment ID or WP_Comment object.
1. $deprecated — Optional. (null) => null
The deprecated.

Usage

  1. if ( !function_exists( 'wp_notify_postauthor' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // Comment ID or WP_Comment object. 
  5. $comment_id = null; 
  6.  
  7. // The deprecated. 
  8. $deprecated = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_notify_postauthor($comment_id, $deprecated); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/pluggable.php  
  1. function wp_notify_postauthor( $comment_id, $deprecated = null ) { 
  2. if ( null !== $deprecated ) { 
  3. _deprecated_argument( __FUNCTION__, '3.8.0' ); 
  4.  
  5. $comment = get_comment( $comment_id ); 
  6. if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) 
  7. return false; 
  8.  
  9. $post = get_post( $comment->comment_post_ID ); 
  10. $author = get_userdata( $post->post_author ); 
  11.  
  12. // Who to notify? By default, just the post author, but others can be added. 
  13. $emails = array(); 
  14. if ( $author ) { 
  15. $emails[] = $author->user_email; 
  16.  
  17. /** 
  18. * Filters the list of email addresses to receive a comment notification. 
  19. * By default, only post authors are notified of comments. This filter allows 
  20. * others to be added. 
  21. * @since 3.7.0 
  22. * @param array $emails An array of email addresses to receive a comment notification. 
  23. * @param int $comment_id The comment ID. 
  24. */ 
  25. $emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID ); 
  26. $emails = array_filter( $emails ); 
  27.  
  28. // If there are no addresses to send the comment to, bail. 
  29. if ( ! count( $emails ) ) { 
  30. return false; 
  31.  
  32. // Facilitate unsetting below without knowing the keys. 
  33. $emails = array_flip( $emails ); 
  34.  
  35. /** 
  36. * Filters whether to notify comment authors of their comments on their own posts. 
  37. * By default, comment authors aren't notified of their comments on their own 
  38. * posts. This filter allows you to override that. 
  39. * @since 3.8.0 
  40. * @param bool $notify Whether to notify the post author of their own comment. 
  41. * Default false. 
  42. * @param int $comment_id The comment ID. 
  43. */ 
  44. $notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID ); 
  45.  
  46. // The comment was left by the author 
  47. if ( $author && ! $notify_author && $comment->user_id == $post->post_author ) { 
  48. unset( $emails[ $author->user_email ] ); 
  49.  
  50. // The author moderated a comment on their own post 
  51. if ( $author && ! $notify_author && $post->post_author == get_current_user_id() ) { 
  52. unset( $emails[ $author->user_email ] ); 
  53.  
  54. // The post author is no longer a member of the blog 
  55. if ( $author && ! $notify_author && ! user_can( $post->post_author, 'read_post', $post->ID ) ) { 
  56. unset( $emails[ $author->user_email ] ); 
  57.  
  58. // If there's no email to send the comment to, bail, otherwise flip array back around for use below 
  59. if ( ! count( $emails ) ) { 
  60. return false; 
  61. } else { 
  62. $emails = array_flip( $emails ); 
  63.  
  64. $switched_locale = switch_to_locale( get_locale() ); 
  65.  
  66. $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); 
  67.  
  68. // The blogname option is escaped with esc_html on the way into the database in sanitize_option 
  69. // we want to reverse this for the plain text arena of emails. 
  70. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  71. $comment_content = wp_specialchars_decode( $comment->comment_content ); 
  72.  
  73. switch ( $comment->comment_type ) { 
  74. case 'trackback': 
  75. /** translators: 1: Post title */ 
  76. $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n"; 
  77. /** translators: 1: Trackback/pingback website name, 2: website IP, 3: website hostname */ 
  78. $notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  79. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  80. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 
  81. $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n"; 
  82. /** translators: 1: blog name, 2: post title */ 
  83. $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title ); 
  84. break; 
  85. case 'pingback': 
  86. /** translators: 1: Post title */ 
  87. $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n"; 
  88. /** translators: 1: Trackback/pingback website name, 2: website IP, 3: website hostname */ 
  89. $notify_message .= sprintf( __('Website: %1$s (IP: %2$s, %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  90. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  91. $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 
  92. $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n"; 
  93. /** translators: 1: blog name, 2: post title */ 
  94. $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title ); 
  95. break; 
  96. default: // Comments 
  97. $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n"; 
  98. /** translators: 1: comment author, 2: author IP, 3: author domain */ 
  99. $notify_message .= sprintf( __( 'Author: %1$s (IP: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; 
  100. $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n"; 
  101. $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n"; 
  102. $notify_message .= sprintf( __('Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n"; 
  103. $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n"; 
  104. /** translators: 1: blog name, 2: post title */ 
  105. $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title ); 
  106. break; 
  107. $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n"; 
  108. $notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment ) ) . "\r\n"; 
  109.  
  110. if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) { 
  111. if ( EMPTY_TRASH_DAYS ) { 
  112. $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 
  113. } else { 
  114. $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 
  115. $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n"; 
  116.  
  117. $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); 
  118.  
  119. if ( '' == $comment->comment_author ) { 
  120. $from = "From: \"$blogname\" <$wp_email>"; 
  121. if ( '' != $comment->comment_author_email ) 
  122. $reply_to = "Reply-To: $comment->comment_author_email"; 
  123. } else { 
  124. $from = "From: \"$comment->comment_author\" <$wp_email>"; 
  125. if ( '' != $comment->comment_author_email ) 
  126. $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>"; 
  127.  
  128. $message_headers = "$from\n" 
  129. . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; 
  130.  
  131. if ( isset($reply_to) ) 
  132. $message_headers .= $reply_to . "\n"; 
  133.  
  134. /** 
  135. * Filters the comment notification email text. 
  136. * @since 1.5.2 
  137. * @param string $notify_message The comment notification email text. 
  138. * @param int $comment_id Comment ID. 
  139. */ 
  140. $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID ); 
  141.  
  142. /** 
  143. * Filters the comment notification email subject. 
  144. * @since 1.5.2 
  145. * @param string $subject The comment notification email subject. 
  146. * @param int $comment_id Comment ID. 
  147. */ 
  148. $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID ); 
  149.  
  150. /** 
  151. * Filters the comment notification email headers. 
  152. * @since 1.5.2 
  153. * @param string $message_headers Headers for the comment notification email. 
  154. * @param int $comment_id Comment ID. 
  155. */ 
  156. $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID ); 
  157.  
  158. foreach ( $emails as $email ) { 
  159. @wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers ); 
  160.  
  161. if ( $switched_locale ) { 
  162.  
  163. return true;