facebook - prevent duplicate comments using wp_insert_comment

admin2025-05-31  0

i am trying to import facebook comments to wordpress what iam doing is get the graph of the facebook post Comments, json decode it then i import the comment via this code

so my code so far is

foreach($comm_no as $answer_id => $v) {
$time = current_time('mysql');
$data = array(
'comment_post_ID' => $post->ID,
'comment_author' => $v->from->name,
'comment_author_email' => '',
'comment_author_url' => '/'.$v->from->id,
'comment_content' => $v->message,
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 5,
'comment_author_IP' => '127.0.0.1',
'comment_agent' => 'egysp',
'comment_date' => $time,
 'comment_approved' => 1,
);
wp_insert_comment($data);`

this code is in my single php

the proplem is every time the post is visisted the comments imported again an again an again so i have like 1000 comment of duplicate comments

can any one help me with this ?

i am trying to import facebook comments to wordpress what iam doing is get the graph of the facebook post Comments, json decode it then i import the comment via this code

so my code so far is

foreach($comm_no as $answer_id => $v) {
$time = current_time('mysql');
$data = array(
'comment_post_ID' => $post->ID,
'comment_author' => $v->from->name,
'comment_author_email' => '',
'comment_author_url' => 'https://www.facebook/'.$v->from->id,
'comment_content' => $v->message,
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 5,
'comment_author_IP' => '127.0.0.1',
'comment_agent' => 'egysp',
'comment_date' => $time,
 'comment_approved' => 1,
);
wp_insert_comment($data);`

this code is in my single php

the proplem is every time the post is visisted the comments imported again an again an again so i have like 1000 comment of duplicate comments

can any one help me with this ?

Share Improve this question edited Jan 13, 2015 at 5:45 Shady M Rasmy asked Dec 25, 2014 at 3:24 Shady M RasmyShady M Rasmy 4044 silver badges13 bronze badges 4
  • Have you considered using wp cron? It can be used to schedule events to run at certain time or interval of times. – Karun Commented Dec 25, 2014 at 5:36
  • Yes and wp secluded event ,but no use .if i used cron or event it will import the comments again every time , i want tobuse wp_filter_comment but idont know the proper way to use it – Shady M Rasmy Commented Dec 25, 2014 at 7:02
  • 2 Literally if you import comments repeatedly then they will be imported repeatedly. You need to decide which precisely are conditions that make comment duplicate and how to handle it. Making requests to a remote system on every load is probably horrible for performance too btw. – Rarst Commented Dec 25, 2014 at 19:07
  • @Rarst that waht i am tring to do for over a week now , and literally I Have no idea what ican Do next . – Shady M Rasmy Commented Dec 25, 2014 at 21:08
Add a comment  | 

1 Answer 1

Reset to default 0 +50

Please Note: Doing this will be very slow! It would be much better to do one (or preferably both) of the following instead:

  1. Use wp_cron to do this on a regular basis
  2. Instead of grabbing all comments, the first time it runs set at timestamp in the options table after it completes. Each time afterwards, read the timestamp and grab just those comments made since the timestamp... and update the timestamp on completion

That being said:

To do this, you should check for duplicates before adding the comment, which requires deciding what constitutes a duplicate comment.

Assuming the author_url and the comment itself would work, you could do something like the following:

function my_comment_already_exists($author_url, $comment) {
    $already_exists = false;
    global $wpdb;
    # try grabbing the first 40 characters of the comment. Hopefully that will make it unique
    # also changed select from 1 as it_exists to comment_ID in case prepare broke that part
    $comment_bit = substr($comment, 0, 40);
    $like_bit = $wpdb->esc_like( $comment_bit ) . '%';
    $query = "SELECT comment_ID FROM {$wpdb->prefix}comments WHERE comment_author_url = %s AND comment_content LIKE %s";
    $query = $wpdb->prepare( $query, $author_url, $like_bit );
    $results = $wpdb->get_results( $query );
    if ( is_null($results) ) {
       # error
       if ( $wpdb->last_error ) {
           echo "<p>Error: {$wpdb->last_error}</p>";
       }
    } else if ( is_array($results) && 0 < count($results) ) {
        $already_exists = true;
    }
    return $already_exists;
}

Then, for each comment, you could do something like:

if ( !my_comment_already_exists( 'https://www.facebook/'.$v->from->id, $v->message ) ) {
    wp_insert_comment($data);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748631938a313661.html

最新回复(0)