WordPress WPML Comments Filter Plugin
28.6.2010 | 2 minutes of reading time
I admit, this post is a bit “off-topic”. Recently we migrated this blog from using qTranslate to WPML for publishing in German and English. Main reasons were much better updates and a cleaner separation.
But one feature was missing because of that: We want to have comments from both languages below the postings (which are now two posts, where they have been one before). WPML doesn’t support this out of the box, but we were able to do this easily ourselves.
Because we believe in Open Source, here the sourcecode of our comment-merging plugin. If required one could also make languages configurable, but we did not need that, and it would have introduced some more complexity.
WPML comment merging plugin on wordpress.org
1<?php
2/*
3Plugin Name: WPML comment merging
4Plugin URI: http://wordpress.org/extend/plugins/wpml-comment-merging/
5Description: This plugin merges comments from all translations of the posts and pages, so that they all are displayed on each other. Comments are internally still attached to the post or page they were made on.
6Version: 1.3
7Author: Fabian Lange
8Author URI: http://blog.codecentric.de/en/2010/06/wordpress-wpml-comments-filter-plugin/
9License: MIT
10*/
11
12function sort_merged_comments($a, $b) {
13 return $a->comment_ID - $b->comment_ID;
14}
15
16function merge_comments($comments, $post_ID) {
17 // get all the languages for which this post exists
18 $languages = icl_get_languages('skip_missing=1');
19 $type = is_page($post_ID) ? 'page' : 'post';
20 foreach($languages as $l) {
21 // in $comments are already the comments from the current language
22 if(!$l['active']) {
23 $otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
24 $othercomments = get_comments( array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC') );
25 $comments = array_merge($comments, $othercomments);
26 }
27 }
28 if ($languages) {
29 // if we merged some comments in we need to reestablish an order
30 usort($comments, 'sort_merged_comments');
31 }
32 return $comments;
33}
34
35function merge_comment_count($count, $post_ID) {
36 // get all the languages for which this post exists
37 $languages = icl_get_languages('skip_missing=1');
38 $type = is_page($post_ID) ? 'page' : 'post';
39
40 foreach($languages as $l) {
41 // in $count is already the count from the current language
42 if(!$l['active']) {
43 $otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
44 if($otherID) {
45 // cannot use call_user_func due to php regressions
46 if ($type == 'page') {
47 $otherpost = get_page($otherID);
48 } else {
49 $otherpost = get_post($otherID);
50 }
51 if ($otherpost) {
52 // increment comment count using translation post comment count.
53 $count = $count + $otherpost->comment_count;
54 }
55 }
56 }
57 }
58 return $count;
59}
60
61add_filter('comments_array', 'merge_comments', 100, 2);
62add_filter('get_comments_number', 'merge_comment_count', 100, 2);
More articles
fromFabian Lange
Your job at codecentric?
Jobs
Agile Developer und Consultant (w/d/m)
Alle Standorte
Gemeinsam bessere Projekte umsetzen.
Wir helfen deinem Unternehmen.
Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.
Hilf uns, noch besser zu werden.
Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.
Blog author
Fabian Lange
Do you still have questions? Just send me a message.
Do you still have questions? Just send me a message.