WordPress WPML Comments Filter Plugin

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

<?php
/*
Plugin Name: WPML comment merging
Plugin URI: http://wordpress.org/extend/plugins/wpml-comment-merging/
Description: 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.
Version: 1.3
Author: Fabian Lange
Author URI: http://blog.codecentric.de/en/2010/06/wordpress-wpml-comments-filter-plugin/
License: MIT
*/
 
function sort_merged_comments($a, $b) { 
	return $a->comment_ID - $b->comment_ID;
}
 
function merge_comments($comments, $post_ID) {
	// get all the languages for which this post exists
	$languages = icl_get_languages('skip_missing=1');
	$type = is_page($post_ID) ? 'page' : 'post';
	foreach($languages as $l) {
		// in $comments are already the comments from the current language
		if(!$l['active']) {
			$otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
			$othercomments = get_comments( array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC') );
			$comments = array_merge($comments, $othercomments);
		}
	}
	if ($languages) {
		// if we merged some comments in we need to reestablish an order
		usort($comments, 'sort_merged_comments');
	}
	return $comments;
}
 
function merge_comment_count($count, $post_ID) {
	// get all the languages for which this post exists
	$languages = icl_get_languages('skip_missing=1');
	$type = is_page($post_ID) ? 'page' : 'post';
 
	foreach($languages as $l) {
		// in $count is already the count from the current language
		if(!$l['active']) {
			$otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
			if($otherID) {
				// cannot use call_user_func due to php regressions
				if ($type == 'page') {
					$otherpost = get_page($otherID);
				} else {
					$otherpost = get_post($otherID);
				}
				if ($otherpost) {
					// increment comment count using translation post comment count.
					$count = $count + $otherpost->comment_count;
				}
			}
		}
	}
	return $count;
}
 
add_filter('comments_array', 'merge_comments', 100, 2);
add_filter('get_comments_number', 'merge_comment_count', 100, 2);
  • Facebook
  • Delicious
  • Digg
  • StumbleUpon
  • Reddit
  • Blogger
  • LinkedIn
Fabian Lange

28 Responses to WordPress WPML Comments Filter Plugin

  1. Amir Helzer says:

    Good solution.

    We didn’t include something like that in the plugin, but what you’ve implemented is just fine.

    You can use icl_get_languages to get the list of other posts in other languages (instead of hard-coding it) and then release as a plugin on wp.org.

  2. Fabian Lange says:

    Hi Amir,
    thanks for the advice. Did not think I could use icl_get_languages, but it works fine.

    I updated the code snippet here and also released the plugin on wp.org:

    http://wordpress.org/extend/plugins/wpml-comment-merging/

  3. Pingback: MaD´s Blog

  4. Gas says:

    Hi Fabian,

    Thanks for the comment merging function for WPML.

    I tried to use it on a 3 language page under development, which has a Guestbook type of page, and should have merged comments in all the languages, but it doesn’t work after installing the plugin through WP admin. Should I put something in the functions.php file or comments.php?

    The link:
    http://www.baksagaspar.com/poli/guestbook/

    THanks for your help.

    Best regards,
    gaspar

  5. Gas says:

    Sorry, the link changed to :
    http://www.polinapasztircsak.com/guestbook/
    (because it’s now opened to widepublic)
    THe comment merging still doesn’t work though… I tried also to turn off the other plugins, reinstalling the comment merger, but nothing helped.

    Tschuss,
    gaspar

  6. Gas says:

    Never mind, I made the comment merging with jQuery AJAX.

    Tschuss,
    gaspar

  7. Hi,

    Your plugin is great but is he merging COMMENTS for PAGE POST? I think no…
    I’ve installed, reinstalled, like 5x times until I realize this.

    Are you going to update it? Or have I made something wrong?

    Thank you!

  8. Hi,

    I’ve Not found the solution!

    I’d have the comments appear in all the languages for the posts AND for PAGES POST.
    I download the pluging “Wmpl Comment Mergin”, it works really good but only for “normal” posts. It doesn’t work for “PAGE POST”.

    The problem with the plugin is that it only merge the comments for “post” not for “page”.

    This is a post I wrote:
    (english) http://www.polyglot-abc.com/flashcards-ultimate-guide-1000-words/
    (spanish) http://www.polyglot-abc.com/es/flashcards-ultima-guia-1000-palabras/
    The plugin works great. The comments are in both languages.

    This is a Page I wrote:
    (english) http://www.polyglot-abc.com/why-is-it-impossible-to-become-polyglot/
    (spanish) http://www.polyglot-abc.com/es/por-que-es-imposible-hacerse-poliglota/
    The plugin doesn’t merge the comments because it’s a page.

    How to make the plugin works with “PAGES”?

    Thank you!

  9. Fabian Lange says:

    Thanks for your comments. I released version 1.2 right now which merges also comments for pages. The code in this blog has been updated and the new version should appear on your wordpress update tab any moment.

  10. Thank you so much! Great plugin!

  11. Gas says:

    Thanks, it works now!!

    Cheers,
    Gaspar

  12. jessor says:

    just a heads up here:
    somehow call_user_func() has changed in newer php releases. you use it in line 45 of wpml-comment-merging.php and it will produce “Warning: Parameter 1 to get_post() expected to be a reference, value given in…” while displaying comment count in the themes.

    this will probably occur on all machines running debian and updating to squeeze’s php which just got released ;)

    a simple fix is to just write a condition like if($type=post) and call the functions directly.

    cheers

  13. Patricia says:

    Hi, thanks for this plugin. It does offer a nice and clean solution. I just have one issue. I run my multiple languages on several domains. My comments count is ok but if I click the link for see the comments, nothing appears on my english version. My german version looks totally fine but my english version not. Can I send you a PM somehow to send you some URL for better understanding?

    Thanks!

  14. 3dolab says:

    Could you please confirm that it’s working with the latest 2.3+ versions of WPML?
    I can get the correctly merged comment_count but not the list.

    It seems there is a comments_array filter that prevents the visitors from seeing
    comments in other languages.
    Anyone already put his hands in the inc/comment-translation/functions.php?

  15. Toine says:

    Hi there, thanks for your good work so far!

    However, your plugin isn’t working for me (yet). I’m using WPML 2.3.4 and want to merge all the comments on a guestbook page. I have the same issue as 3dolab, the comment_count is working, but only the comments of the current language show up? Any ideas or update soon?
    Thanks!

  16. Fabian Lange says:

    Sorry guys, but at the moment there is no way to make the plugin work with the newest wpml anymore.
    The comment filtering broke the display, as it prevents getting comments in any other than the current language.
    For this blog, we hacked it, so its working for us, but i cannot recommend that.

    • Toine says:

      Hi Fabian, thanks for your answer.
      Is it very difficult to hack it? And will you still be able to make updates on the WPML plugin after the hack?
      Thanks!

      • Fabian Lange says:

        Hi Toine,
        there quite a few places in wpml which you would need to change. everything which hooks into the regular comment mechanism. you need to redo this after every wpml upgrade. i might find a new way to do this, but i really hope for wpml to get a switch to completely turn off the comment translation code.
        Fabian

  17. We’ve got the comment merging working with the latest WordPress and WPML for all post types. I’ve posted about it on the WordPress support forums.

    Thanks for your work on this plugin, Fabian, it’s great!

  18. Jan says:

    Hi,

    I’ve been trying to get this to work but without success.
    I’ve placed some echo’s in the code to see what’s doing what.
    The comment count is correct (comments of all languages summed up).

    but the comment merging isn’t working.
    It’s:
    – retrieving the languages codes of the languages currently not being displayed
    – it can get the postID of the current post in other language

    BUT
    when it does the get_comments() function with the other lang PostID passed along it retrieves nothing :( :( :(
    when I hardcode postID to the current post in current lang it just loops the current comments x-1 languages.

    so the code seems to work for current language posts only.
    It seems that get_comments() does not want to retrieve comments of the same post in other lang.

    would appreciate a work-around / help if anyone knows how to fix this :)

    Cheers!

    • Jan says:

      by replacing:
      $othercomments = get_comments( array(‘post_id’ => $otherID, ‘status’ => ‘approve’, ‘order’ => ‘ASC’) );

      BY:
      $othercomments = get_approved_comments($otherID);

      I got it to work again! :)

      • Matthias says:

        Hi Jan!
        For me your solution only works as long as I am logged in as admin.
        Did you try it already logged out?

  19. Fabian Lange says:

    Hi Jan,
    yeah I have to admit that it is in a hacky state right now. I am not very pleased with the way WPML interacts with comments. Glad you got it working!

    Fabian

  20. Patricia BT says:

    Hi Fabian

    As your plugin is not working on latest WP or WPML, Harshad from WPML has made a slight modification, I listed it there in the 3rd comment
    http://wpml.org/forums/topic/merge-comments-i-need-harshads-modification/#post-123518

    maybe you can just update it in the WP.org repository?
    (or do you prefer not to do so because you are not pleased by the current code?)

    anyway, for me this plugin is a lifesaver so I thank you very much.

    Patricia

    • Fabian Lange says:

      Hi Patricia,

      so that patch is working fine? I will have a look and update the plugin. I kind of lost the interest, due to the upgrade policy and comments by wpml. we are running now on our own fork of wpml, which is not having these commercial translation offers hooked up everywhere.

      Fabian

  21. Jennifer says:

    Hallo Fabian – ich verstehe, dass ihr kein Interesse an einem Update habt. Die ganzen Posts die rumschwirren und dein Plugin updaten funktionieren leider bei mir nicht. Gäbe es noch eine Chance, dass ihr das Original Plugn aktualisiert ? Das wäre wirklich großartig !
    Jennifer

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>