Skip to content

Instantly share code, notes, and snippets.

@umdevelopera
Created January 10, 2024 19:28
Show Gist options
  • Save umdevelopera/f7b0e07d5db870c9ce9fc1e513224e45 to your computer and use it in GitHub Desktop.
Save umdevelopera/f7b0e07d5db870c9ce9fc1e513224e45 to your computer and use it in GitHub Desktop.
Example. A function used to translate choices of the Checkbox field in the Ultimate Member form. Uses the Polylang language switcher.
<?php
// Translate choices of the Checkbox field.
function my_checkbox_item_title( $title, $key, $v ) {
$lang = pll_current_language();
$texts = array(
'um_pet' => array(
'en' => array(
'Cat' => 'Cat',
'Dog' => 'Dog',
'Bird' => 'Bird',
'Rat' => 'Rat',
),
'es' => array(
'Cat' => 'Gato',
'Dog' => 'Perro',
'Bird' => 'Pájaro',
'Rat' => 'Rata',
),
),
);
if ( array_key_exists( $key, $texts ) ) {
if ( array_key_exists( $lang, $texts[ $key ] ) ) {
if ( array_key_exists( $title, $texts[ $key ][ $lang ] ) ) {
$title = $texts[ $key ][ $lang ][ $title ];
}
}
}
return $title;
}
add_filter( 'um_field_checkbox_item_title', 'my_checkbox_item_title', 10, 3 );
function my_checkbox_item_title_view( $value, $data, $type ) {
if ( 'checkbox' === $type && is_array( $value ) ) {
$key = $data['metakey'];
foreach ( $value as &$title ) {
$title = my_checkbox_item_title( $title, $key, $title );
}
}
return $value;
}
add_filter( 'um_profile_field_filter_hook__', 'my_checkbox_item_title_view', 10, 3 );
@umdevelopera
Copy link
Author

Choices in the Ultimate Member fields are not translatable. You can use this workaround to translate choices of the Checkbox field.
In this code I translated choices of the Pet um_pet field. Change the $texts array for the field and choices you need.

The field settings example
Example - Translate choices of the Checkbox field

You can use this solution in addition to the the Ultimate Member - Polylang plugin.

Related:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment