marcoR-2
Hi!
I have created a few custom fields associated to a page.
This custom fields are stored in a custom table.
In WPML -> settings -> custom fields translation are not showing.
How can I make it work?
Thank You
Ahmed Mohammed
Supporter
Timezone:
Africa/Cairo (GMT+02:00)
Hi there,
Thank you for contacting WPML support. While you are waiting for one of my colleagues to take this ticket and work on it, let me provide you with the first debugging steps or if I can help with the issue quickly.
WPML → Settings → Custom Fields translation will show the custom fields stored in the wp_postmeta table.
To translate the data added to custom tables, then you will need to use WPML hooks to translate them.
Here is the list of hooks: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
I would suggest you take a look at these 4 hooks first to see if it helps:
- https://wpml.org/wpml-hook/wpml_register_single_string/
- https://wpml.org/wpml-hook/wpml_translate_single_string/
- https://wpml.org/wpml-hook/wpml_register_string/
- https://wpml.org/wpml-hook/wpml_translate_string/
Using the above hook you can register the strings for translations and translate them from WPML->String Translations and use another hook to display it on the front end.
Also, to know more about WPML tables, please check this documentation https://wpml.org/documentation/support/wpml-tables/
Let me know if this helps.
marcoR-2
Hi!
Thank you for your answer.
I end up with this code, but I still not see anything.
<?php
function register_custom_field_for_translation() {
global $wpdb;
$table_name = $wpdb->prefix . 'sl_admin';
$field_name = 'sl_admin_text';
do_action('wpml_register_single_string', 'sl_texts', 'Custom', $table_name . ':' . $field_name, $field_name);
}
add_action('wpml_register_string', 'register_custom_field_for_translation');
function translate_custom_field($value, $original_value, $context, $language_code) {
global $wpdb;
$table_name = $wpdb->prefix . 'sl_admin';
$field_name = 'sl_admin_text';
if ($context === 'Custom' && $original_value === $table_name . ':' . $field_name) {
$translated_value = apply_filters('wpml_translate_single_string', $value, 'sl_texts', 'Custom', $table_name . ':' . $field_name, $language_code);
return $translated_value;
}
return $value;
}
add_filter('icl_translate', 'translate_custom_field', 10, 4);
Ahmed Mohammed
Supporter
Timezone:
Africa/Cairo (GMT+02:00)
Hi Marco,
In WPML support, we're not developers; unfortunately, we cannot help with custom coding.
With that said, I can see that you're trying to register a string package. We have a detailed example here: https://wpml.org/documentation/support/string-package-translation/ .
Please give it a try and let us know how it goes.
Regards,