Sync comments on all translation posts using WPML

 Why Choose WPML?

There are many great WordPress translation plugins on the market, but there are many valid reasons to choose WPML.

The first reason is compatibility. WPML is a stable solution that’s been proven to work with thousands of different WordPress products. WordPress plugins in their test area to ensure it integrates with all WordPress plugins.

This is why so many WordPress themes and plugins proudly declare they’re ‘Compatible with WPML’.

Now let’s move to our actual issue with WPML which may require some efforts to complete our requirement.

As basically all posts are different with there id and work individually but some how they are connected when we translate on other language so we can say it’s translation posts of some specific post. Comments are associated with the posts in the language in which they were created.

For example: When we have WPML installed with 3 different languages options: English (default), French, Spanish other any other languages. When user submit comments on one specific language post. Unfortunately, those comments are displayed only in their respective post’s languages.

Suppose if we have post in English with post_id = 101 and translation post French have post_id= 102 and Spanish language with same post have post_id = 103

In above scenario, if we want that when user submit post comments in post_id = 101, it must display in 102,103 also then we must translate comments in this both post also.

it will not be possible to sync or translate the comments between the original language and translated language posts when we are using the default Comments feature of WordPress

There are two below solution available to resolve for issue:

  • Enable settings from WPML plugin settings

This solution is only work we have post translated in other language but we are not edit posts content individually so make we have same content in all translation post also

How can I do that?

If we have latest wpml then we need to enable comments synch option in below screen

WPML settings


 Translation management -> Multilingual settings -> Post and pages sync ->sync comments for duplicated content

  • Custom code

This solution work in any scenario like no matter we have enabled WPML settings option or not and post content we are translating individually or not 

Here is an example below for custom code implement in function.php file:


/*       add_action( 'comment_post', 'duplicate_comment_insert_func' , 990 );
         
 function 'duplicate_comment_insert_func( $comment_id ) {
                             global $wpdb, $sitepress;                      
                             $newcomment= array();   
       
                      $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID=%d", $comment_id ), ARRAY_A );
                             $newcomment= $comment;                    
                             $post_id = $comment['comment_post_ID'];      
          
   $language_details_original = $sitepress->get_element_language_details($post_id, 'post_post');
    $langcode_array = array(); $counts = 0;
    
       foreach($sitepress->get_active_languages() as $lang => $details){
         if($lang != $language_details_original->language_code){
                                                         $langcode_array[$counts] = $lang;
                                                         $ counts ++;
                                           }
        }
      
          for($i=0;$i<count($langcode_array);$i++){
                         
                             $langcode =  $langcode_array[$i];
                             $dup_id =  icl_object_id($post_id,'post',false,$langcode);
                             $post_language_information = wpml_get_language_information($post_id);
                             $newcomment['comment_post_ID'] = $dup_id;
               
                    if($comment['comment_parent']){
$ispostid = $wpdb->get_var (
                                           $wpdb->prepare (
                                           "SELECT meta_id
                                                FROM {$wpdb->commentmeta}
                                                WHERE `meta_value`= %d and meta_key`='_comment_duplicate_of_'" ,  $comment['comment_parent']  )
                             );

$translated_postid =  icl_object_id($comment['comment_post_ID'],'post',false,$langcode);

if((!$ispostid) && ($i==0)){                                                                   
$translated_parent = $wpdb->get_var (
$wpdb->prepare (
  "SELECT c.comment_id
   FROM {$wpdb->commentmeta} cm
   JOIN {$wpdb->commentmeta} cmb
  ON ( cmb.meta_value = cm.meta_value
           AND cmb.meta_key = cm.meta_key)
  OR cm.comment_id = cmb.meta_value
   JOIN {$wpdb->comments} c
  ON c.comment_ID = cmb.meta_value
      WHERE cm.meta_key = '_comment_duplicate_of_'
  AND ( cm.comment_id = %d OR cm.meta_value = %d )
  AND c.comment_post_ID = %d",
  $comment[ 'comment_parent' ],
  $comment[ 'comment_parent' ],
  $dup_id
                   )
   );           
  }else{
$translated_parent = $wpdb->get_var (
$wpdb->prepare (
 "SELECT c.comment_id
   FROM {$wpdb->commentmeta} cm
     JOIN {$wpdb->commentmeta} cmb
                                                    ON ( cmb.meta_value = cm.meta_value
                                                    AND cmb.meta_key = cm.meta_key)
                                                    OR cm.comment_id = cmb.meta_value
                                                    JOIN {$wpdb->comments} c
                                                    ON c.comment_ID = cmb.comment_id
      WHERE cm.meta_key = '_comment_duplicate_of_'
      AND ( cm.comment_id = %d OR cm.meta_value = %d )
       AND c.comment_post_ID = %d",
 $comment[ 'comment_parent' ],
 $comment[ 'comment_parent' ],
 $dup_id
 )
);                                                                                                                              
  }                                                                                                                 
                  $newcomment['comment_parent']= $translated_parent;
              }             
$newcomment_ID = wp_insert_comment( $newcomment );
add_comment_meta( $newcomment_ID, '_comment_duplicate_of_', $comment_id, true );                                  }
        return $comment_id;      
   }
   */

Hope this code will useful for you to resolve the comment related issue as it’s already working for me.

All the best, enjoy your day and please share your review on this solution.

 

 

Previous
Next Post »