comment_exists

Determine if a comment exists based on author and date.

Description

comment_exists( (string) $comment_author, (string) $comment_date, (string) $timezone = 'blog' ); 

For best performance, use $timezone = gmt'', which queries a field that is properly indexed. The default value for $timezone is blog for legacy reasons.

Parameters (3)

0. $comment_author (string)
Author of the comment.
1. $comment_date (string)
Date of the comment.
2. $timezone — Optional. (string) => 'blog'
Timezone. Accepts blog or gmt.. Default blog .

Usage

  1. if ( !function_exists( 'comment_exists' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/comment.php'; 
  3.  
  4. // Author of the comment. 
  5. $comment_author = ''; 
  6.  
  7. // Date of the comment. 
  8. $comment_date = ''; 
  9.  
  10. // Timezone. Accepts 'blog' or 'gmt'. Default 'blog'. 
  11. $timezone = 'blog'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = comment_exists($comment_author, $comment_date, $timezone); 
  15.  

Defined (1)

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

/wp-admin/includes/comment.php  
  1. function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) { 
  2. global $wpdb; 
  3.  
  4. $date_field = 'comment_date'; 
  5. if ( 'gmt' === $timezone ) { 
  6. $date_field = 'comment_date_gmt'; 
  7.  
  8. return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments 
  9. WHERE comment_author = %s AND $date_field = %s",  
  10. stripslashes( $comment_author ),  
  11. stripslashes( $comment_date ) 
  12. ) );