• Resolved somePaulo

    (@finomeno)


    Hello!
    Relevanssi is indexing English display_names for authors on non-english posts, Russian in our case.

    WordPress/WPML saves default user details as English with translations available through the WPML String Translations plugin. All our users’ first, last, and display names are correctly and fully translated through WPML and appear correctly on the front end in the post metas box: in English on English posts, and in Russian on Russian posts. However, when indexing posts, Relevanssi is picking up only the English display_names for all posts irrespective of the post language, so searching for an author’s name in Russian on the Russian version of the site yields no results.

    I’ve tried setting up a filter based on examples from Relevanssi and WPML (see code below), but it doesn’t do what I want. How do I make Relevanssi index the Russian display_name translations for Russian posts?

    Code tried (in current theme’s functions.php):

    add_filter( 'relevanssi_post_author', 'rlv_translated_author_names', 10, 2 );
    function rlv_translated_author_names( $post_author, $post ) {
    if(ICL_LANGUAGE_CODE=='ru') {
    $post_author = apply_filters( 'wpml_translate_single_string', $post_author, 'Authors', 'Display Name', 'ru' );
    }
    return $post_author;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    The filter seems good from Relevanssi point of view; I can’t say anything about the WPML side because I’ve never tried translating user profiles in WPML. How does the function fail? If the problem is that wpml_translate_single_string doesn’t return the correct Russian display name, you’re better off asking the question from WPML support. They know better how to translate the display names.

    If the problem is that ICL_LANGUAGE_CODE isn’t ru when indexing the post, you can try something else instead of that. Relevanssi uses this to determine the post language: $post_language = apply_filters( 'wpml_post_language_details', null, $post_id );

    Thread Starter somePaulo

    (@finomeno)

    Thank you for your suggestion.

    I was expecting Relevanssi to pick up string translations from WPML’s wp_icl_string_translations DB table by default. Since it doesn’t, I tried myself, and then asked you when I failed. Ive compiled the code I shared above from Relevanssi (https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_post_author/) and WPML (https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-617933) docs respectively, and I’m not sure which part of it doesn’t work. We don’t have access to WPML support since the plugin came bundled with a paid theme, without a separate license needed to get support from them.

    I’ve tried your suggestion to determine the post language (see below) and even tried skipping the if statement altogether, leaving just the $post_author = ... line in the function, making sure to rebuild the index after each try. This didn’t help. All our editors are aware of how to correctly publish articles, and they all make sure to set the correct WPML language for posts before they do anything else. I’ve tried creating a new post myself too, but it didn’t help either.

    Updated code:
    add_filter( 'relevanssi_post_author', 'rlv_translated_author_names', 10, 2 );
    function rlv_translated_author_names( $post_author, $post ) {
    $post_language = apply_filters( 'wpml_post_language_details', null, $post_id );
    if( $post_language['language_code'] == 'ru' ) {
    $post_author = apply_filters( 'wpml_translate_single_string', $post_author, 'Authors', 'Display Name', 'ru' );
    }
    return $post_author;
    }

    As a workaround, I’ve added the Russian names to Relevanssi synonyms. That works for the frontend search, but might not be a good solution in the long term since we have quite a lot of authors, and the team is expanding (the site is an online magazine), so having to duplicate translations in synonyms is extra work and error-prone.

    WPML allows to translate the first_name, last_name, display_name and bio by default, requiring manual intervention to translate other user profile fields. So maybe Relevanssi could check for those four defaults having translations? Or maybe you have some other suggestions I might try?

    Plugin Author Mikko Saari

    (@msaari)

    I figured it out. The name of the field is not “Display Name”; it’s “display_name_XX”, where “XX” is the user ID. This worked for me:

    add_filter( 'relevanssi_post_author', 'rlv_translated_author_names', 10, 2 );
    function rlv_translated_author_names( $post_author, $post ) {
    $post_language = apply_filters( 'wpml_post_language_details', null, $post->ID );
    if ( $post_language['language_code'] == 'ru' ) {
    $post_author = apply_filters( 'wpml_translate_single_string', $post_author, 'Authors', 'display_name_' . $post->post_author, 'ru' );
    }
    return $post_author;
    }
    Thread Starter somePaulo

    (@finomeno)

    Thank you so much! It works.

    WPML’s example is not very clear and makes it look like their filter is expecting the actual text name of the field that appears on the user profile page, not the DB name of that field.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.