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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 17:00 10:00 – 17:00 10:00 – 17:00 10:00 – 17:00 10:00 – 17:00 -
- 18:00 – 19:00 18:00 – 19:00 18:00 – 19:00 18:00 – 19:00 18:00 – 19:00 -

Supporter timezone: Asia/Kathmandu (GMT+05:45)

Tagged: 

This topic contains 20 replies, has 3 voices.

Last updated by Shekhar Bhandari 1 year, 5 months ago.

Assisted by: Shekhar Bhandari.

Author Posts
December 14, 2023 at 11:13 pm #15069067

Lars Eiben

I got the following setup:
Bricksbuilder + WPML + ACF + ACFE

I have an acf group with a field called product_colors which is a term field ( colors ) of a taxonomy ( specs )
The taxonomy is set to translateable and the terms are correctly translated.
i have set the return format to object/term in the acf field.
Furthermore, the field is setup as clone / copy always - to make sure the data is always up to date if i change the main post. ( this is not a field i want to have custom translated - it should always be in sync )

i have this function to get the name of the term :

function getACFObejctName( string $acf_field, $post_id = null ) {
if (is_null($post_id)) {
$post_id = get_bricks_query_object_id();
}

$field = get_field($acf_field, $post_id);

return $field ? $field->name : null;
}

im using this via {echo: getACFObejctName('product_colors', POSTID) } ( this is the bricks format ) or directly via php code.
This works as expected on pages / posts.

Now i have a modal on this page here:
hidden link

just click on the compare button and choose a product, or add it directly via the grid, to compare products. This happens via ajax + custom functions to get and display the data.
Im using the base post_id and a cookie to sync everything and then depending on the locale im getting the translated post_id.
With this translated post_id i then want to get the data via my function. Now comes the problem:

when i use "return name" in the acf field settings - i only get the default ( german ) value back. When i use object, i get the german object data back. when i use id, i get the german id back.
I can modify my code and add something like this:

$field = get_field($acf_field, $post_id);

if ( get_locale() != 'de_DE' ) {

$translated_id = apply_filters( 'wpml_object_id', $field->term_id, $field->taxonomy, TRUE, 'en' );
$field = get_term( $translated_id, $field->taxonomy );

}

this then works with the acf object return format, but not with string/name format or id.
I dont understand why i have no issues when using the function on a page, but via ajax its not returning the translated term.

( i have added the get_locale() code part for 1 function and left it out for the other so you can see the difference in the modal result )

regards

December 18, 2023 at 11:36 am #15083751

Lars Eiben

Maybe this is related to the issue.

In any language which is not the default one, the translated terms are not displayed / loaded in the backend. It works on the frontend for some reason but the issue is, i cant edit anything in the translated post, because these fields then are empty / null and either it wont let me save the post cuz they are mandatory or they are null and then remove the term for some reason and some other fields are depended on the term chosen in some fields.

I attached an image for a backend post for a translation.

Software is a taxonomy / terms that is NOT translated, as these names are unique and wont change in any language.

Hersteller ( Brand ) is translated as these will be displayed on the homepage with their own site + text etc and these terms have themselves custom fields too

Hardware Typ is translated and only has 2 terms – Mouse or Mousepad, they are used for the permalink structure.

All of these fields dont have a hirachy – but i have other fields that do and there is the exact same issue as soon as they are translated

As you can see it shows the original term id, but wont add the translated term to the acf field, and when saving the post, those terms wont be added to the post, even though the original term id seems still to be linked.

regards

image.png
December 18, 2023 at 5:00 pm #15086453

Alejandro
WPML Supporter since 02/2018

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hello!

I'll help you get started while a supporter takes your case.

I'm not entirely sure of exactly everyhing you're doing but doing something in PHP vs doing it in AJAX is not the same thing.

PHP VS AJAX is already outside the scope of our support but it seems that with PHP you can get an object but not an ID, for example is that correct?

There are a few things we can do to improve your current code (we do not support debugging so all i'm writing here should be tested and adapted by you).

1) to get the current language, use this hook and not the locale condition: https://wpml.org/wpml-hook/wpml_current_language/

2) Use the approach suggested by WP when it comes to ajax calls for things done in PHP and maybe this Stack Overflow page could serve as a guide: https://wordpress.stackexchange.com/questions/313643/how-to-call-a-php-function-with-ajax-when-the-user-clicks-a-button

and maybe this ajax guide would also help you out: https://codex.wordpress.org/AJAX_in_Plugins

In a nutshell, you write your PHP code, and then using ajax, you "link" to it. but i believe you're already doing all this in the standard way and i'm adding all this info so you can confirm it.

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

For the rest, i'd greatly appreciate if you could send me the WPML debug information?
You can follow this guide to find it: https://wpml.org/faq/provide-debug-information-faster-support/

this is because i want to see how you have configured the ACF field groups and other info.

Make sure you select one of the 2 options mentioned here:
https://wpml.org/documentation/related-projects/translate-sites-built-with-acf/#selecting-a-translation-option

And with the ACF field group untranslated and with the "do not translate" setting under WPML> Settings > Post type translation.

December 18, 2023 at 7:01 pm #15086961

Lars Eiben

Hey,

yeah the ajax / php stuff is working i just wanted to mention where the problems occure.
My "feeling" right now is the following (as both the frontend modal stuff not being translated and the backend terms not being "matched"):

For any acf field that uses a taxonomy / term: as soon as i translate this term, doesnt matter if i choose the wpml option to only use translations or to fallback if no translation exists, it will use the "base" language term. This behaviour would explain y these fields are emtpy in the backend ( like i said i have set em up to be a copy of the original to keep them in sync ) and of course in the ajax / php part, it would get the "original" object / id from the acf field even though i request the field with the id of the translated post. (my code is 100% getting the correct post_id depending on the frontend language, i 3pl checked this)
I tryed get_locale() and i tryed the wpml filter to get the current language too, they were both correct for en_US on frontend.
In the end this should not be needed because the translated term object, id or name (however the return type of the field is setup) should be saved to the post or not?

When i dont use a taxonomy / term field but lets say a text field, and this field is translated, i get the correct translated value back for example. So all this stuff is working as expected, but not the terms if those are translated + the acf field set copy always

Debug infos - where can i send them or is this thread private and i can just add them here ?

regards

December 18, 2023 at 7:08 pm #15086969

Lars Eiben

And here are 2 screenshots for the wpml / acf settings

wpml_1.jpg
wpml_2.jpg
December 20, 2023 at 5:51 am #15097717

Shekhar Bhandari
WPML Supporter since 03/2015

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello Lars,

I will be helping you out further on this issue.

I have gone through your message multiple times but still, I am unable to figure out exactly the way to replicate so can you correct me if I am wrong on the below steps:

1. Website Setup:
- Create a WordPress website with the following components:
- WPML (WordPress Multilingual Plugin) for multilingual support.
- ACF (Advanced Custom Fields) plugin.
- ACFML

2. Create ACF Group and Field:
- Create an ACF group with a field called "product_colors."
- Configure the "product_colors" field as a term field associated with a taxonomy called "specs."
- Ensure that the "specs" taxonomy is set to be translatable in WPML, and you have correctly translated the taxonomy terms.

3. ACF Field Settings:
- In the ACF field settings for "product_colors":
- Set the "Return Format" to "object/term."
- Configure the field to copy data always to keep it in sync with the main post.

4. Function to Get Term Name:
- Implement a custom PHP function to retrieve the name of the term associated with a post using the "product_colors" field. The function should look something like this:

   function getACFObejctName(string $acf_field, $post_id = null) {
       if (is_null($post_id)) {
           $post_id = get_bricks_query_object_id();
       }

       $field = get_field($acf_field, $post_id);

       return $field ? $field->name : null;
   }
   

5. Usage on Pages/Posts:
- Use the `getACFObejctName` function to display the term name on pages/posts where the ACF field is used, like this:

     {echo: getACFObejctName('product_colors', POSTID)}
     

6. Replicate the Issue in Modal via AJAX:
- Visit the page where you want to replicate the issue.
- Trigger a modal (e.g., by clicking a "Compare" button).
- Use AJAX and custom functions to retrieve and display data in the modal, including the term associated with the post.

7. Issue Replication:
- When you attempt to retrieve the translated term within the modal via AJAX, you should observe the following issues:
- If the ACF field is set to return the term name or ID, it only returns the default (German) value, regardless of the selected language.
- When the ACF field is set to return the term object, you can manually fetch the translated term using additional code, such as the `apply_filters` approach mentioned in the messages.

I have enabled the debug information for you, you can add it in the debug information box while sending a reply.

Look forward to your reply.

Thanks

December 20, 2023 at 1:16 pm #15103085

Lars Eiben

Hey,

ive added the debug info and made a video as i think its more easy to show the setup and explain what i mean so there are no missunderstandings, i hope this is allright for you.
The steps you wrote are correct. But u missed the issue where the copied fields in the translated posts dont have the matching translated terms - but this will be clear with my video 🙂

Video is uploaded to my.hidrive (strato) - if u prefer something else let me know - u can directly watch it in the browser or dl:
hidden link

regards

December 20, 2023 at 2:12 pm #15103351

Shekhar Bhandari
WPML Supporter since 03/2015

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Thank you for the video, even if the selection is shown empty, the database should have the correct values for the terms selected, so I don't think it's the one creating the issue.

I have created a test site here with ACF pro and WPML: hidden link

I created a test ACF field for taxonomies and when I try to check the secondary post editor, the taxonomies are selected properly. You can check it yourself by using the test site.

Being so, do you have a test site, if yes, Have you tried disabling all non-WPML plugins, switching to one of the default themes, and checking whether the issue still persists or not?

Please remember to create a backup of your site (both files and the database) before making any updates or changes. This is always a good practice.

Look forward to your reply.

Thanks

December 20, 2023 at 7:50 pm #15105811

Lars Eiben

Hey,

thank you for the test environment - i guess i found the issue.
I made a video once again and added everything to the test env. so you can check and confirm.

Video (same link): hidden link

If im correct, could you leave the test env. active so i can forward the login to acfe support + my video so they can check it out too?

regards

December 21, 2023 at 6:06 am #15106643

Shekhar Bhandari
WPML Supporter since 03/2015

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

If you enable the WPML editor in the translations preferences the fields are being copied, can you please? check and confirm.

You can use the test environment.

Look forward to your reply.

Thanks

Edit-custom-test-“Test-POst”-‹-Sandbox-—-WordPress.jpg
December 21, 2023 at 11:11 am #15109303

Lars Eiben

Cheers,

i've just tested the editor settings from your screenshot and found another strange behaviour / bug and can't confirm your idea/fix is working. Created a new video once again to show the different things that happen + the strange bug 🙂

Video (same link): hidden link

Ive opened a ticket at acfe now with the test env. - thx for letting me use this while we / they try to get to the bottom of this

regards

December 21, 2023 at 9:36 pm #15113413

Lars Eiben

I did some more testing and found a "workaround" for the saving:

Have a look at the new post with the name BUG "FIX"

I created a new tax wich hirarchy + added 2 new acf fields with different selection - one with "all child terms" and one with direct selection of the allowed terms. When i now use load terms in the acf field, it will load the term from the meta ( but only from the current translated post_id and only for the acf field where the terms are directly selected ).
When i remove or change the value in the field itself, it will NOT get the term from the original post, which it does for the normal taxonomy field.

Problem with this is on the frontend, when i use get_field it now loads ALL the terms in that taxonomy that are related to the post, so with the hirarchy and those special term restrictions there is either another bug or just not working for what i would like to have. If this "load terms" option would still use the restriction from the field settings i could use it.
I guess i could write another function to get the field settings, check those restrictions and then cross check with every object from the get_field array. But i have the feeling this is something that should be done by acf / acfe when using the native function?

Another question that came up while testing - when such a field is set to copy, why is the field itself in the translation NOT disabled in the backend like they are for text fields for example?

regards

December 22, 2023 at 9:59 am #15115623

Shekhar Bhandari
WPML Supporter since 03/2015

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello Lars,

Another question that came up while testing - when such a field is set to copy, why is the field itself in the translation NOT disabled in the backend like they are for text fields for example?

This has already been reported to our developers and you can find a workaround for this here: https://wpml.org/forums/topic/acfml-custom-taxonomy-fields-set-to-copy-are-editable-in-non-original-language/#post-13823943

I wanted to ask something about of things we are doing here:
- Why are you using a manual editor, we recommend using WPML editors so you can just translate the content without editing it manually.

- Can you translate one-page using WPML editors and see if it helps? You can check on the front end.

The correct workflow is to use "Load Terms" and duplicate, but you mentioned it doesn't work on hierarchy one, can you make a short video on it?

Look forward to your reply.

Thanks

December 22, 2023 at 3:19 pm #15119427

Lars Eiben

Hey,

Will check the fix out thx.

Im using the manual editor because some fields cant be translated / changed with the one from wpml ( i mention this in video 5 ) - e.g. i have a oembed field for a yt video - this is a different one but cant be "translated" with wpml editor. Or some taxonomy fields need completly different terms. The UVP / RRP Price is different for € (EU) and $ (US) etc.

Video 4 does showcase the problem with the hierarchy and load fields etc.

I used the wpml editor in video 5 and tryed to cover as much i could find from all the different behaviours we have tested in this thread.

Video (same link): hidden link

In summary, i have no idea right now which of these "bugs / behaviours" are intended, and an issue of wpml, and which are a matter for acf or acfe tbh.
For me right now the optimal "fix" would be either that the correct terms are loaded in the normal editor for tax with or without hierarchy or that when i use "load terms" that i only get the terms that are saved/selected in the acf field back and not every term that is saved as meta field to the post.

Sorry for the longer videos, i just want to be as pricise as i can so you dont have to waste time on how to replicate the issue.

regards and thank you for your support as right now i would have to manually select every field which is a massive pain in the ass for over 300 posts and counting up 🙂

Cheers

December 25, 2023 at 5:38 am #15125391

Shekhar Bhandari
WPML Supporter since 03/2015

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Thank you for the video.

I would suggest testing on the test site until we fix the issue, it's hard to debug on your site configuration since it's complex, can you create a small hierarchy of terms on the test site and see if you can replicate it?

Also, please try the solution mentioned earlier to lock the fields and see if it helps.

Look forward to your reply.

Thanks

The topic ‘[Closed] ACF Fields not returning translated value – custom ajax function’ is closed to new replies.