Skip Navigation

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.

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by taiyoF 1 year, 9 months ago.

Assisted by: Eran Helzer.

Author Posts
July 7, 2023 at 5:35 am #13964059

taiyoF

I am trying to: Make a multilingual blog post page using Blog Posts Block on my site.

Link to a page where the issue can be seen: hidden link
1. click "load more" > English blogs are loaded: correct
2. change the language to Japanese > Japanese blogs are loaded: correct
3. click "さらに投稿を読み込む" > English blogs are loaded << Wrong!

I expected to the "load more" button should have URL query "&lang=ja", but not.

July 7, 2023 at 7:49 am #13965309

Eran Helzer
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

Can you please provide your WPML debug information? You can see exactly how to do that here:
https://wpml.org/faq/provide-debug-information-faster-support/

From looking at the network tab while loading more posts in Japanese, I see that the

Please try enabling the following setting, if you haven't already. This is useful when loading content using backend calls, as the "Load More" button does.
Go to WPML -> Languages -> Language filtering for AJAX operations. You can read more about what exactly this does in our documentation here:
https://wpml.org/documentation/getting-started-guide/language-setup/enabling-language-cookie-to-support-ajax-filtering/

July 7, 2023 at 8:45 am #13966161

taiyoF

Thank you for your message.

The problem, loading the wrong language causes language query lacking, goes on after I set Language filtering for AJAX operations.

On WebInspector, the network tab tells me that &lang=ja lacks on the Load More button.
I pasted the Request URL pushed from the button below, and confirmed if I add "&lang=ja" in this URL, correct language feeds are loaded.

Request URL:
hidden link

July 7, 2023 at 9:11 am #13966479

Eran Helzer
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Yes I see.

This may be caused by several factors. I would like to request admin access to your site, in order to have a look. I will not be making any changes whatsoever, just looking.

I have set your next reply to be private so you can share credentials there.
Remember that it is always better to create a new admin user for me, then when we are done you can delete it.

July 11, 2023 at 6:09 am #14010067

Eran Helzer
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Perfect, thank you.

I can't manage to log in, I believe because of Jetpack. Would it be possible to disable it temporarily while I investigate the issue?

July 11, 2023 at 9:13 am #14012107

taiyoF

Thank you for your check. I set the Jatpack login management feature off.
I'm sorry I don't know how to deactivate entire JetPack in WordPress.com, but log-in falure would be solved.

July 12, 2023 at 5:15 am #14018691

Eran Helzer
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Great, that worked.

After some investigating, I managed to find the source of the issue and a possible solution.
It seems like this has come up once before, this ticket:
https://wpml.org/forums/topic/do-not-consider-wp-wpml_current_admin_language_hash-on-frontend-wp-rest-api/

The issue is that the language the Blog Posts uses is not the current frontend language, rather the wpml_current_admin_language_{hash} cookie language. If you want to see, clear the cookies from the site, open the page while your admin language is in Japanese and then posts will load in Japanese.

This solution would be to add a function to the functions.php file, which upon rendering the Button block, alters the data-next attribute to add the lang={current language) parameter.

If you can provide me with FTP credentials I will attempt a solution, however it is important to explain that this is outside of our support policy, and thus we will not take any responsibility for maintaining or providing further support for it. This will be just a friendly suggestion to help you out.

July 12, 2023 at 5:29 am #14018703

taiyoF

Thank you! It's great information.
Do you mean you're going to add the following code? If so, I'll do it myself.

function weichie_load_more() {
  $ajaxposts = new WP_Query([
    'post_type' => 'publications',
    'posts_per_page' => 6,
    'orderby' => 'date',
    'order' => 'DESC',
    'paged' => $_POST['paged'],
    'lang' => pll_current_language(),
  ]);

  $response = '';

  if($ajaxposts->have_posts()) {
    while($ajaxposts->have_posts()) : $ajaxposts->the_post();
      $response .= get_template_part('parts/card', 'publication');
    endwhile;
  } else {
    $response = '';
  }

  echo $response;
  exit;
}

add_action('wp_ajax_weichie_load_more', 'weichie_load_more');
add_action('wp_ajax_nopriv_weichie_load_more', 'weichie_load_more');
July 12, 2023 at 7:06 am #14019231

Eran Helzer
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

That's also interesting, haven't though of that.

Actually, I was thinking more along the lines of:

add_filter( 'render_block', 'modify_newspack_blocks_homepage_articles', 10, 2 );

function modify_newspack_blocks_homepage_articles( $block_content, $block ) {
    if ( 'newspack-blocks/homepage-articles' !== $block['blockName'] ) {
        // Not the block we're interested in.
        return $block_content;
    }

    // Use a DOM parser to modify the HTML content.
    $dom = new DOMDocument();
    @$dom->loadHTML( mb_convert_encoding( $block_content, 'HTML-ENTITIES', 'UTF-8' ), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
    
    // Find the element with the data-next attribute.
    $xpath = new DOMXPath( $dom );
    $elements = $xpath->query( '//*[@data-next]' );
    $current_lang = apply_filters( 'wpml_current_language', NULL );

    foreach ( $elements as $element ) {
        $url = $element->getAttribute( 'data-next' );

        // Add your parameter to the URL.
        $new_url = add_query_arg( 'lang', $current_lang, $url );

        // Update the attribute.
        $element->setAttribute( 'data-next', $new_url );
    }

    // Save the modified HTML and return it.
    $block_content = $dom->saveHTML();
    return $block_content;
}

This code finds the Load More button element and adds the lang parameter to the data-next attribute.

This approach is usually not recommended as it could impact site performance, so I suggest you test out both approaches and choose the least impactful option.

July 13, 2023 at 7:30 am #14029331

taiyoF

Thank you Eran,
Your code did not solve the problem, but I'm going to fix by modifying the Load More button directory.

Your information helped me a lot. Thank you.

July 13, 2023 at 7:31 am #14029345

taiyoF

Thank you.