wp_allow_comment

Validates whether this comment is allowed to be made.

Description

(int|string|WP_Error) wp_allow_comment( (array) $commentdata, (bool) $avoid_die = false ); 

Returns (int|string|WP_Error)

Allowed comments return the approval status (0|1|'spam'). If `$avoid_die` is true, disallowed comments return a WP_Error.

Parameters (2)

0. $commentdata (array)
Contains information on the comment.
1. $avoid_die — Optional. (bool) => false
When true, a disallowed comment will result in the function returning a WP_Error object, rather than executing wp_die(). Default false.

Usage

  1. if ( !function_exists( 'wp_allow_comment' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Contains information on the comment. 
  5. $commentdata = array(); 
  6. $avoid_die = false; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = wp_allow_comment($commentdata, $avoid_die); 
  10.  

Defined (1)

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

/wp-includes/comment.php  
  1. function wp_allow_comment( $commentdata, $avoid_die = false ) { 
  2. global $wpdb; 
  3.  
  4. // Simple duplicate check 
  5. // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) 
  6. $dupe = $wpdb->prepare( 
  7. "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",  
  8. wp_unslash( $commentdata['comment_post_ID'] ),  
  9. wp_unslash( $commentdata['comment_parent'] ),  
  10. wp_unslash( $commentdata['comment_author'] ) 
  11. ); 
  12. if ( $commentdata['comment_author_email'] ) { 
  13. $dupe .= $wpdb->prepare( 
  14. "AND comment_author_email = %s ",  
  15. wp_unslash( $commentdata['comment_author_email'] ) 
  16. ); 
  17. $dupe .= $wpdb->prepare( 
  18. ") AND comment_content = %s LIMIT 1",  
  19. wp_unslash( $commentdata['comment_content'] ) 
  20. ); 
  21.  
  22. $dupe_id = $wpdb->get_var( $dupe ); 
  23.  
  24. /** 
  25. * Filters the ID, if any, of the duplicate comment found when creating a new comment. 
  26. * Return an empty value from this filter to allow what WP considers a duplicate comment. 
  27. * @since 4.4.0 
  28. * @param int $dupe_id ID of the comment identified as a duplicate. 
  29. * @param array $commentdata Data for the comment being created. 
  30. */ 
  31. $dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata ); 
  32.  
  33. if ( $dupe_id ) { 
  34. /** 
  35. * Fires immediately after a duplicate comment is detected. 
  36. * @since 3.0.0 
  37. * @param array $commentdata Comment data. 
  38. */ 
  39. if ( true === $avoid_die ) { 
  40. return new WP_Error( 'comment_duplicate', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 ); 
  41. } else { 
  42. if ( wp_doing_ajax() ) { 
  43. die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); 
  44.  
  45. wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 ); 
  46.  
  47. /** 
  48. * Fires immediately before a comment is marked approved. 
  49. * Allows checking for comment flooding. 
  50. * @since 2.3.0 
  51. * @since 4.7.0 The `$avoid_die` parameter was added. 
  52. * @param string $comment_author_IP Comment author's IP address. 
  53. * @param string $comment_author_email Comment author's email. 
  54. * @param string $comment_date_gmt GMT date the comment was posted. 
  55. * @param bool $avoid_die Whether to prevent executing wp_die() 
  56. * or die() if a comment flood is occurring. 
  57. */ 
  58. 'check_comment_flood',  
  59. $commentdata['comment_author_IP'],  
  60. $commentdata['comment_author_email'],  
  61. $commentdata['comment_date_gmt'],  
  62. $avoid_die 
  63. ); 
  64.  
  65. /** 
  66. * Filters whether a comment is part of a comment flood. 
  67. * The default check is wp_check_comment_flood(). See check_comment_flood_db(). 
  68. * @since 4.7.0 
  69. * @param bool $is_flood Is a comment flooding occurring? Default false. 
  70. * @param string $comment_author_IP Comment author's IP address. 
  71. * @param string $comment_author_email Comment author's email. 
  72. * @param string $comment_date_gmt GMT date the comment was posted. 
  73. * @param bool $avoid_die Whether to prevent executing wp_die() 
  74. * or die() if a comment flood is occurring. 
  75. */ 
  76. $is_flood = apply_filters
  77. 'wp_is_comment_flood',  
  78. false,  
  79. $commentdata['comment_author_IP'],  
  80. $commentdata['comment_author_email'],  
  81. $commentdata['comment_date_gmt'],  
  82. $avoid_die 
  83. ); 
  84.  
  85. if ( $is_flood ) { 
  86. return new WP_Error( 'comment_flood', __( 'You are posting comments too quickly. Slow down.' ), 429 ); 
  87.  
  88. if ( ! empty( $commentdata['user_id'] ) ) { 
  89. $user = get_userdata( $commentdata['user_id'] ); 
  90. $post_author = $wpdb->get_var( $wpdb->prepare( 
  91. "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",  
  92. $commentdata['comment_post_ID'] 
  93. ) ); 
  94.  
  95. if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) { 
  96. // The author and the admins get respect. 
  97. $approved = 1; 
  98. } else { 
  99. // Everyone else's comments will be checked. 
  100. if ( check_comment
  101. $commentdata['comment_author'],  
  102. $commentdata['comment_author_email'],  
  103. $commentdata['comment_author_url'],  
  104. $commentdata['comment_content'],  
  105. $commentdata['comment_author_IP'],  
  106. $commentdata['comment_agent'],  
  107. $commentdata['comment_type'] 
  108. ) ) { 
  109. $approved = 1; 
  110. } else { 
  111. $approved = 0; 
  112.  
  113. $commentdata['comment_author'],  
  114. $commentdata['comment_author_email'],  
  115. $commentdata['comment_author_url'],  
  116. $commentdata['comment_content'],  
  117. $commentdata['comment_author_IP'],  
  118. $commentdata['comment_agent'] 
  119. ) ) { 
  120. $approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam'; 
  121.  
  122. /** 
  123. * Filters a comment's approval status before it is set. 
  124. * @since 2.1.0 
  125. * @param bool|string $approved The approval status. Accepts 1, 0, or 'spam'. 
  126. * @param array $commentdata Comment data. 
  127. */ 
  128. $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata ); 
  129. return $approved;