Home › Forums › General Issues › Translating ACF form field labels in frontend with WPML
A follow-up for this forum post about translating form field labels with Polylang. This time with WPML String Translation.
add_action('init', function(){
if (!is_admin()) {
foreach (acf_get_field_groups() as $group) {
$fields = acf_get_fields($group['ID']);
if (is_array($fields) && count($fields)) {
foreach ($fields as $field) {
do_action('wpml_register_single_string', 'ACF Labels', $field['label'], $field['label']);
do_action('wpml_register_single_string', 'ACF Messages', $field['message'], $field['message']);
do_action('wpml_register_single_string', 'ACF Instructions', $field['instructions'], $field['instructions']);
}
}
}
}
});
add_filter('acf/load_field', function (array $field) {
$field['label'] = apply_filters('wpml_translate_single_string', $field['label'], 'ACF Labels', $field['label']);
$field['message'] = apply_filters('wpml_translate_single_string', $field['message'], 'ACF Messages', $field['message']);
$field['instructions'] = apply_filters('wpml_translate_single_string', $field['instructions'], 'ACF Instructions', $field['instructions']);
return $field;
});
Hi!
I love you solution, and it have helped me alot!
However I’m struggeling with labels from fields in groups. Have you found a solution for that?
I fixed it by hardcoding jquery translation-strings 🙂
Eg:
<?php
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if($my_current_lang == 'en') {
?>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script>
$( document ).ready(function() {
$("form .class").html($("form .class").html().replace('Original string','New string'));
</script>
<?php
}
?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.