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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | - | 9:00 – 18:00 | 9:00 – 18:00 | 9:00 – 18:00 | 9:00 – 18:00 | 9:00 – 18:00 |
- | - | - | - | - | - | - |
Supporter timezone: America/Lima (GMT-05:00)
Tagged: Custom Work
This topic contains 12 replies, has 1 voice.
Last updated by Andreas W. 3 days, 17 hours ago.
Assisted by: Andreas W..
Author | Posts |
---|---|
September 12, 2025 at 4:57 pm #17398460 | |
John |
Background of the issue: Symptoms: Questions: |
September 12, 2025 at 5:25 pm #17398504 | |
John |
The AI suggested: $en = apply_filters('wpml_translate_single_string', get_option('blogname'), 'admin_texts_blogname', 'blogname', null, 'en'); $cy = apply_filters('wpml_translate_single_string', get_option('blogname'), 'admin_texts_blogname', 'blogname', null, 'cy'); But this returns the same as bloginfo('name'). Basically, on teh English site, both strings are English, and on the Welsh site, both strings are Welsh. |
September 12, 2025 at 6:22 pm #17398556 | |
Andreas W. WPML Supporter since 12/2018 Languages: English (English ) Spanish (Español ) German (Deutsch ) Timezone: America/Lima (GMT-05:00) |
Hello, Could you please try using: add_action( 'init', function() { $blogname = get_option('blogname'); do_action( 'wpml_register_single_string', 'admin_texts_blogname', 'blogname_en', $blogname ); do_action( 'wpml_register_single_string', 'admin_texts_blogname', 'blogname_cy', $blogname ); }); }); This registers the site name as two separate strings in WPML’s String Translation—one for English and one for Welsh. Then, in your template, you can retrieve both translations like this: $blogname = get_option('blogname'); $en = apply_filters( 'wpml_translate_single_string', $blogname, 'admin_texts_blogname', 'blogname_en' ); $cy = apply_filters( 'wpml_translate_single_string', $blogname, 'admin_texts_blogname', 'blogname_cy' ); This should ensure you get the correct version of the site name regardless of the current language context. Best regards |
September 12, 2025 at 6:22 pm #17398586 | |
John |
I have also tried: apply_filters('wpml_translate_single_string', bloginfo('name'), 'WP', 'Blog Title', 'en'); as this matches up better with the documentation at https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/?wpv_view_count=602355&wpvhookcategory=retrieving-localized-content&wpv-hook=0&wpv_post_search&wpv_filter_submit=Submit and the values in the String Translation section, but the result is the same. In addition, the above code echoes the strings directly, without needing to php echo them. |
September 12, 2025 at 6:24 pm #17398602 | |
Andreas W. WPML Supporter since 12/2018 Languages: English (English ) Spanish (Español ) German (Deutsch ) Timezone: America/Lima (GMT-05:00) |
Seems we replied at the same moment. Please review my earlier comment and give this a try. |
September 12, 2025 at 8:06 pm #17398647 | |
John |
No, that doesn't work either... There are new entries in the String translation section, but blogname_en and blogname_cy have the same English name. Putting a Welsh translation in for them just does the same as putting in a Welsh translation for the original blogname, ie shows the same language as the rest of the site. All I want to do is something like get_string_translation ($name, $domain, $lang_code);. |
September 12, 2025 at 9:02 pm #17398738 | |
Andreas W. WPML Supporter since 12/2018 Languages: English (English ) Spanish (Español ) German (Deutsch ) Timezone: America/Lima (GMT-05:00) |
My apologies, I forgot to mention that you can change the source language for strings on WPML > String Translation: Anyhow, it seems my proposal was not 100% correct, as instead we should register the strings only once. You will then translate this newly registered string on WPML > String Translation and then call this newly registered string in your template. add_action( 'init', function () { $blogname = get_option( 'blogname' ); do_action( 'wpml_register_single_string', 'site_settings', 'blogname', $blogname ); }); Translate the newly registered string. To call it in the template, use: $title = apply_filters( 'wpml_translate_single_string', get_option( 'blogname' ), // default/fallback 'site_settings', // context used above 'blogname' // name used above ); echo esc_html( $title ); I tested this code successfully. |
September 12, 2025 at 9:04 pm #17398739 | |
John |
Actually, edit to the above, blogname_en translates correctly, but blogname_cy stays in Welsh all the time |
September 12, 2025 at 9:27 pm #17398741 | |
John |
We seem to be doing lots of replying at the same time. In response to #17398738 above, your code only outputs one string. I want to output both languages. ENGLISH SITE NAME and this should be the same whether the rest of the site is in English or Welsh, ie it should stay exactly the same when the language switcher is pressed. |
September 12, 2025 at 9:44 pm #17398789 | |
John |
... and to clarify what I mean above, I want to output the original string AND the string translation at the same time... and what is output should NOT be translated when the language is switched. |
September 12, 2025 at 11:58 pm #17398809 | |
Andreas W. WPML Supporter since 12/2018 Languages: English (English ) Spanish (Español ) German (Deutsch ) Timezone: America/Lima (GMT-05:00) |
Thank you for your feedback! The string translation from English to Welsh can only show up on the Welsh part of the website. Due to language filtering, it is not possible to display a string that was translated into Welsh on the English website. You would need to register the strings manually with fixed source values: add_action( 'init', function() { do_action( 'wpml_register_single_string', 'site_settings', 'blogname_en', 'Your Site Name in English' ); do_action( 'wpml_register_single_string', 'site_settings', 'blogname_cy', 'Your Site Name in Welsh' ); }); This will register both strings with the English source language, as they are supposed to show up on the English website. If needed, you translated them on WPML > String Translation, but even if they are not translated, the following code should display the same strings in all languages. To retrieve the strings on the frontend, use: $en = apply_filters( 'wpml_translate_single_string', 'Your Site Name in English', 'site_settings', 'blogname_en' ); $cy = apply_filters( 'wpml_translate_single_string', 'Your Site Name in Welsh', 'site_settings', 'blogname_cy' ); echo esc_html( $en ) . ' ' . esc_html( $cy ); This worked on my test site without issues. |
September 13, 2025 at 11:09 am #17399225 | |
John |
Ok, Thanks for your replies, but all that seems a bit convoluted. I don't want to register new strings for content that WordPress already stores. In the end, I worked out a solution which uses the string translations provided by WordPress core, so it should work universally. I am putting it here for others' reference, should they want the same thing. (Apologies, I don't seem to have options for markdown or adding code, so the code is below, with relevant comments after) ``` ob_start(); $en = apply_filters( 'wpml_translate_single_string', $blogname, 'WP', 'Blog Title', 'en' ); echo $en; ?> // To get the string rather than echo it, an output buffer is required. // then the wpml_translate_string filter works as documented in https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/?wpv_view_count=602355&wpvhookcategory=retrieving-localized-content&wpv-hook=0&wpv_post_search&wpv_filter_submit=Submit // the variables can then be echoed as normal |
September 13, 2025 at 2:52 pm #17399336 | |
Andreas W. WPML Supporter since 12/2018 Languages: English (English ) Spanish (Español ) German (Deutsch ) Timezone: America/Lima (GMT-05:00) |
Thank you for following up! I am glad to hear you found a solution for this purpose. |