Support » Plugin: Polylang » Show all comments for all languages

  • Hi,

    I see this has been a problem for many reding different threads and I tried what Chouby gave as a filer in one of those, which is:

    function polylang_remove_comments_filter() {
        global $polylang;
        remove_filter('comments_clauses', array(&$polylang->filters, 'comments_clauses'));
    }
    add_action('wp','polylang_remove_comments_filter');

    but it didn’t work and reading other posts with code in it I was able to get something that works.

    My hope now is that someone validate this if everything is fine, if it can be optimized and maybe if Chouby is reading that he can simply add this as an option that would make it fully supported by the plugin.

    /*** Show all comments for all languages ***/
    
    //Remove the filter
    function polylang_remove_comments_filter() {
        global $polylang;
        remove_filter('comments_clauses', array(&$polylang, 'comments_clauses'));
    }
    
    function sort_merged_comments($a, $b) {
    	return $a->comment_ID - $b->comment_ID;
    }
    
    //Get the comments from the same post in different languages
    function merge_comments($comments, $post_ID) {
    	global $sitepress;
    	remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ) );
    	// get all the languages for which this post exists
    	$languages = icl_get_languages();
    	$post = get_post( $post_ID );
    	$type = $post->post_type;
    	foreach($languages as $code => $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');
    	}
    	//
    	add_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ) );
    
    	return $comments;
    }
    
    //Add comments for the same posts in different languages
    function merge_comment_count($count, $post_ID) {
    	// get all the languages for which this post exists
    	$languages = icl_get_languages();
    	$post = get_post( $post_ID );
    	$type = $post->post_type;
    
    	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;
    }
    
    //Trigger the functions
    add_action('wp','polylang_remove_comments_filter');
    add_filter('comments_array', 'merge_comments', 100, 2);
    add_filter('get_comments_number', 'merge_comment_count', 100, 2);

    This was tested with posts only but I see pages should be supported.

    https://wordpress.org/plugins/polylang/

Viewing 4 replies - 16 through 19 (of 19 total)
  • jonathanmoorebcsorg

    (@jonathanmoorebcsorg)

    yes it’s a separate function, I just had it in my theme functions.php

    Rhialto

    (@rhialto)

    Ok so count on website is fine and comments display correctly in both language but count on admin panel is wrong.

    I have made a little plugin, called Polylang Comments Merger, starting from the old wpml plugin. It works in the AdminCP (showing the right number of comments per post) and in the frontend. You can download it from here:

    https://github.com/rbnet/polylang-comments-merger

    It should work in posts and pages post-type.

    Bye.

    Hey this is great! I deleted everything about previous code in my functions.php and added your plugin instead, works great.

    Thanks a lot!

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘Show all comments for all languages’ is closed to new replies.