This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 6 replies, has 4 voices.

Last updated by martinW-30 3 years, 9 months ago.

Assigned support staff: Joanna.

Author Posts
October 10, 2016 at 7:04 am #1111672

francescoP-20

Hi there!
I am using WPML to provide 2 different languages options: English (default) and Italian. Some pages have comments, and visitors can leave comments to pages in any language. Unfortunately, those comments are displayed only in their respective pages' languages.

Here is an example.
I have "Page A" in double language. I visit the first language (Page A - English) and leave a comment (EN Comment). Then I go to the other language (Page A - Italian) and leave a different comment (IT Comment).

Now, here is what I expected to see -->
Page A - English:
- EN Comment;
- IT Comment;
Page A - Italian:
- EN Comment;
- IT Comment;

Instead this is what I get -->
Page A - English:
- EN Comment;
Page A - Italian:
- IT Comment;

So the problem is that, when I call the comments of the page, WordPress returns only the comments that match the language where they were originally posted in. Instead I need to view ALL the comments available for a specific page in each and every translated page.

----------------

One more thing.
I tried everything. Disabling other plugins, deleting other functions from the functions.php file, using a blank theme. Nothing changed. So I wanted to narrow down the problem and I tried to do something very specific: I called a specific comment by its ID and I discovered that even in this way I cannot get that comment in both languages. That comment will only appear in its original language. Here is the code I used:

$comment__in = array( 28,29 ); // IDs of the 2 different comments, posted in the 2 different translated pages
$args = array(
	'comment__in' => $comment__in
);
$comments = get_comments($args);
foreach ( $comments as $comment ) {
	echo $comment->comment_content;
}

----------------

I currently use WordPress 4.6.1 and WPML Multilingual CMS 3.5.3.1

I hope I provided enough information for now, but let guys me know if you need anything else! Thanks in advance and I hope I will hear back from you soon!

Francesco

October 11, 2016 at 7:19 am #1112552

Joanna

Hello @francescop-20,

WPML filters any content by the currently active language. This is why you couldn't display all comments, even when using the specific IDs. You would need to temporarily switch the currently active language to get comments from other than actually active language.
You may do that using our hooks in the code:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

Having all comments displayed in all languages is not a feature of wpml yet, and such a script would need custom coding which is beyond the scope of our support. This being the case please consider contacting one of our certified partners from this link: http://wpml.org/documentation/support/wpml-contractors/
You will get the custom assistance you need to get on with your project.
Before doing so please have a look also here: http://wpml.org/documentation/support/wpml-contractors/guidelines-for-people-looking-for-consulting-help/

October 11, 2016 at 6:42 pm #1113187

francescoP-20

Hi Joanna,

Thanks for helping me with this issue! What I find curious is that I can easily get any information calling them by ID: titles, contents, hyperlinks, exc. Everything comes through and does not get filtered. Everything but comments. Why this exception? (just curious)

In any case, using the "wpml_switch_language" hook can be pretty handy and I am so thankful you introduced me to it! The only downside with this approach is that I won't be able to process a group of information at the same time, but I'll have to split the queries between the different languages. I can probably find a workaround, but I'm sure it won't be super simple.

Lastly, is there any hook that temporarily disables the wpml filters? I am thinking something like this:

do_action( 'wpml_disable_language_filters', "all" );
//Run queries to get all the content, regardless of the languages where it comes from
do_action( 'wpml_restore_language_filters', "en" );

Does something like this, or anything similar, exists yet? If not, are you guys planning to introduce it in the short future?

Thanks so much again!

October 12, 2016 at 1:28 pm #1113752

Joanna

Hello @francescop-20,

Comments are implemented a bit differently. We constantly work on the code to improve it and this results sometimes that something works with one thing and not with another. It's possible that in the future we will allow displaying comments in all languages in an easy way, however, it's not planned for now.

Regarding the hook for disabling all wpml filters, it doesn't exist for now and we do not plan to implement it in the near future.

October 12, 2016 at 5:03 pm #1113973

francescoP-20

Hi Joanna,

Thanks for explaining the situation. In me meanwhile I was able to find a workaround. Let me know if this is an acceptable solution or if it might create issues in the future updates of the plugin.

// Store the current language in a variable
$current_language_code = ICL_LANGUAGE_CODE;
// If the current language is not the default one, switch to it. This part is necessary to make the code below work in not default languages
if ( $current_language_code !== 'en' ){
	do_action( 'wpml_switch_language', "en" );
}
// Get an array of all the IDs of the translated pages
$languages = icl_get_languages('skip_missing=1');
$id = array();
if( 1 < count($languages) ){
	foreach( $languages as $l ){
		$id[] = apply_filters( 'wpml_object_id', get_the_ID(), 'page', FALSE, $l[language_code]);
	}
}
// Remove filter related to the comments
global $sitepress;
remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10, 2 );
// Call all the comments from all the page IDs
$comments = get_comments(array(
	'orderby' => 'comment_date',
	'order' => 'DESC',
	'post__in' => $id
));
// Print the comments with a custom template (optional)
foreach($comments as $comment) {
	comment_template_sign_up( $comment, $args, $depth );
}
// Switch back to to the current page's original language
if ( $current_language_code !== 'en' ){
	do_action( 'wpml_switch_language', $current_language_code );
}
// Add filter again
add_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10, 2 ); 

If this code is good, I hoe it might help other people.

Thanks again for the support!
Francesco

September 10, 2018 at 1:49 pm #2720106

felixG-6

Hi @francescop-20,

I took your idea and came up with the following solution that fits more into the year 2018 🙂


/**
 * ALSO SHOW COMMENTS FROM OTHER LANGUAGES
 */

global $sitepress;
remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ), 10, 2 );
add_action('pre_get_comments', function($c){
    $id = [];
    $languages = apply_filters('wpml_active_languages', '');
    if( 1 < count($languages) ){
        foreach( $languages as $l ){
            $id[] = apply_filters( 'wpml_object_id', $c->query_vars['post_id'], 'page', FALSE, $l['code']);
        }
    }

    $c->query_vars['post_id'] = '';
    $c->query_vars['post__in'] = $id;
    return $c;
});
September 18, 2018 at 2:14 pm #2745369

martinW-30

Excellent solution - many thanks!