WPML compatability
Opened this issue · 1 comments
Version
- Carbon Fields:3.6.5
- WordPress: 6.6.1
- PHP:8.1.28
Expected Behavior
Container::make('post_meta', __('HTML body post')) ->where( 'post_type', 'IN', array('blog')) ->where('post_template', 'IN', array( 'single-blog.php' )) ->add_fields( array( Field::make( 'complex', 'crb_blog_html_body', __( 'Blog html body' ) ) ->add_fields( array( Field::make( 'rich_text', 'html_block', __( 'Html block' ) ), )), Field::make( 'text', 'crb_blog_faq_title', __( 'Title FAQ' ) ), Field::make( 'complex', 'crb_blog_faq_cards', __( 'Blog FAQ cards' ) ) ->add_fields( array( Field::make( 'text', 'title', __( 'Title' ) ), Field::make( 'text', 'subtitle', __( 'Subtitle' ) ) )), ));
How to properly configure translation via wpml carbon fields ?
https://prnt.sc/lS13t28Uz8hr
The wpml support asked me to contact you directly. The original plan was to specify a “copy” for the main repeater Field::make( 'complex', 'crb_blog_html_body', __( 'Blog html body' ) ) but as you can see on the screenshot it is not present
PS In the wml editor itself you can see all the fields, I've even translated them already. But the repeater fields are still not displayed on the page, in the admin they are also not shown for the translated locale
/** WPML Workaround for compsupp-7642 - Part 1*/add_filter('wpml_filter_field_value', 'wpml_compsupp7642_workaround_for_serialized_post_meta'); function wpml_compsupp7642_workaround_for_serialized_post_meta($field_value) { if (!is_array($field_value)) { return $field_value; } foreach ($field_value as &$single_value) { if (!is_array($single_value)) { continue; } foreach ($single_value as &$value) { if (!is_string($value)) { continue; } if (is_serialized($value)) { $unserialized = @unserialize($value); if ($unserialized !== false) { $value = is_array($unserialized) ? reset($unserialized) : $unserialized; } } } } return $field_value;}
Problem:
If you're trying to translate custom fields with a repeater on your site and they appear in the translation admin panel but not on the site after translation, we have a solution.
Solution:
1- Add this snippet to the functions.php file of the theme:
/*
- WPML Workaround for compsupp-7642 - Part 1
*/
add_filter('wpml_filter_field_value', 'wpml_compsupp7642_workaround_for_serialized_post_meta');
function wpml_compsupp7642_workaround_for_serialized_post_meta($field_value) {
if (!is_array($field_value)) {
return $field_value;
}
foreach ($field_value as &$single_value) {
if (!is_array($single_value)) {
continue;
}
foreach ($single_value as &$value) {
if (!is_string($value)) {
continue;
}
if (is_serialized($value)) {
$unserialized = @unserialize($value);
if ($unserialized !== false) {
$value = is_array($unserialized) ? reset($unserialized) : $unserialized;
}
}
}
}
return $field_value;
}
2- Open the app/public/wp-content/themes/cash4smstheme/page-faq.php file
3- Replace the following snippet:
$crb_page_faq_1_title = carbon_get_post_meta($page_id, 'crb_page_faq_1_title');
$crb_page_faq_1_cards = carbon_get_post_meta($page_id, 'crb_page_faq_1_cards');
$crb_page_faq_2_title = carbon_get_post_meta($page_id, 'crb_page_faq_2_title');
$crb_page_faq_2_cards = carbon_get_post_meta($page_id, 'crb_page_faq_2_cards');
$crb_page_faq_3_title = carbon_get_post_meta($page_id, 'crb_page_faq_3_title');
$crb_page_faq_3_cards = carbon_get_post_meta($page_id, 'crb_page_faq_3_cards');
With
$crb_page_faq_1_cards = apply_filters('wpml_filter_field_value', carbon_get_post_meta($page_id, 'crb_page_faq_1_cards'));
$crb_page_faq_2_title = carbon_get_post_meta($page_id, 'crb_page_faq_2_title');
$crb_page_faq_2_cards = apply_filters('wpml_filter_field_value', carbon_get_post_meta($page_id, 'crb_page_faq_2_cards'));
$crb_page_faq_3_title = carbon_get_post_meta($page_id, 'crb_page_faq_3_title');
$crb_page_faq_3_cards = apply_filters('wpml_filter_field_value', carbon_get_post_meta($page_id, 'crb_page_faq_3_cards'));
The idea is to apply a filter to all fields with the issue. This filter will apply our workaround function (part 1) that removes the double array of the value.
This solution might be outdated or not applicable to your specific case. If these steps do not resolve your issue, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the problem persists, please open a new support ticket.